荟萃馆

位置:首页 > 计算机 > 计算机二级

2017年9月计算机二级考试JAVA考试题及答案

2017年下半年计算机二级考试正在备考中。你知道计算机二级考试JAVA科目都考哪些知识吗?为了方便考生备考计算机二级考试JAVA 科目。下面是小编为大家带来的计算机二级考试JAVA考试题,欢迎阅读

2017年9月计算机二级考试JAVA考试题及答案

  考试题一:

1、编写程序,计算下列分段函数的值。

x? (x>=0)

y=

-x? (x<0)

import .*;

public class testa

{ public static void main(String[] args) throws IOException

{

float? x,y;

InputStreamReader reader=new InputStreamReader();

BufferedReader input=new BufferedReader(reader);

tln("请输入x:");

String temp=Line();

x = eFloat(temp);

if(x>=0)

{

y=x;

}else

{

y=-x;

}

tln("y="+y);

}

}

2、根据年龄,判断某人是否为成年。

import .*;

public class testa

{

public static void main(String[] args) throws IOException

{

int x;

InputStreamReader reader=new InputStreamReader();

BufferedReader input=new BufferedReader(reader);

tln("请输入x:");

String temp=Line();

x = eFloat(temp);

if(x>=18)

{

tln("成年人");

}

if(x<18)

{

tln("未成年");

}

}

}

3、判断2020的奇偶性,并进行输出。

public class test

{

public static void main(String[] args)

{

int x;

x=2020;

if(x%2==0)

tln(+x+"是偶数");

else

tln(+x+"不是偶数");

}

}

4、比较两个数的大小,找出其中的最大数并输出。

import .*;

public class ka

{ public static void main(String[] args) throws IOException

{

float? x,y,m;

m=0;

InputStreamReader readerx=new InputStreamReader();

BufferedReader inputx=new BufferedReader(readerx);

tln("请输入x:");

String tempx=Line();

x = eFloat(tempx);

InputStreamReader readery=new InputStreamReader();

BufferedReader inputy=new BufferedReader(readery);

tln("请输入y:");

String tempy=Line();

y= eFloat(tempy);

if(x>y)

{

m=x;

}else

{

m=y;

}

tln("最大数为"+m);

}

}

5、比较两个数的大小,找出其中的最小数并输出。

import .*;

public class ka

{ public static void main(String[] args) throws IOException

{

float? x,y,m;

m=0;

InputStreamReader readerx=new InputStreamReader();

BufferedReader inputx=new BufferedReader(readerx);

tln("请输入x:");

String tempx=Line();

x = eFloat(tempx);

InputStreamReader readery=new InputStreamReader();

BufferedReader inputy=new BufferedReader(readery);

tln("请输入y:");

String tempy=Line();

y= eFloat(tempy);

if(x{

m=x;

}else

{

m=y;

}

tln("最小数为"+m);

}

}

6、编写一个Java程序,判断某年份是否为闰年。

import .*;

public class testa

{

public static void main(String[] args) throws IOException

{

float? x;

InputStreamReader reader=new InputStreamReader();

BufferedReader input=new BufferedReader(reader);

tln("请输入x:");

String temp=Line();

x =eFloat(temp);

if(x@0==0)

{

tln(+x+"是闰年");

}

else

if(x%4==0)

{

tln(+x+"是闰年");

}

else{ tln(+x+"不是闰年");}

}

}

7、比较两个数的大小,找出其中的最大数和最小数并输出。

import .*;

public class ka

{ public static void main(String[] args) throws IOException

{

float? x,y;

InputStreamReader readerx=new InputStreamReader();

BufferedReader inputx=new BufferedReader(readerx);

tln("请输入x:");

String tempx=Line();

x = eFloat(tempx);

InputStreamReader readery=new InputStreamReader();

BufferedReader inputy=new BufferedReader(readery);

tln("请输入y:");

String tempy=Line();

y= eFloat(tempy);

if(x{

tln("最小数为"+x);

tln("最大数为"+y);

}else

{

tln("最小数为"+y);

tln("最大数为"+x);

}

}

}

8、比较两个数的大小,找出其中的最大数和最小数,并输出最大数和最小数之差。

import .*;

public class ka

{ public static void main(String[] args) throws IOException

{

float? x,y,m;

InputStreamReader readerx=new InputStreamReader();

BufferedReader inputx=new BufferedReader(readerx);

tln("请输入x:");

String tempx=Line();

x = eFloat(tempx);

InputStreamReader readery=new InputStreamReader();

BufferedReader inputy=new BufferedReader(readery);

tln("请输入y:");

String tempy=Line();

y= eFloat(tempy);

if(x{

tln("最小数为"+x);

tln("最大数为"+y);

m=y-x;

tln("最大数与最小数之差为"+m);

}else

{

tln("最小数为"+y);

tln("最大数为"+x);

m=x-y;

tln("最大数与最小数之差为"+m);

}

}

}

9、编写程序,计算下列分段函数的值。

x? (x>0)

y=???? 0? (x=0)

-x? (x<0)

import .*;

public class testa

{

public static void main(String[] args) throws IOException

{

float? x,y;

y=0;

InputStreamReader reader=new InputStreamReader();

BufferedReader input=new BufferedReader(reader);

tln("请输入x:");

String temp=Line();

x = eFloat(temp);

if(x>0)

{

y=x;

}

if(x==0)

{

y=0;

}

if(x<0)

{

y=-x;

}

tln("y="+y);

}

}

10、编写程序,计算下列分段函数的值。

x-1? (x>1)

y=???? 0? (-1≤x≤1)

x+10? (x<-1)

import .*;

public class testa

{

public static void main(String[] args) throws IOException

{

float? x,y;

y=0;

InputStreamReader reader=new InputStreamReader();

BufferedReader input=new BufferedReader(reader);

tln("请输入x:");

String temp=Line();

x = eFloat(temp);

if(x>1)

{

y=x-1;

}

if(1.0>=x&&x>=-1.0)

{

y=0;

}

if(x<-1)

{

y=x+10;

}

tln("y="+y);

}

}

  考试题二:

1、 编写程序,实现1到100之间整数的累加并输出运算结果。

public class a

{

public static void main(String[] args)

{

int i,s=0;

for(i=1;i<=100;i++)

{

s=s+i;

}

tln("1到100的累加和是"+s);

}}

2、编写程序,计算1~100中奇数的累加和并输出。

public class a

{

public static void main(String[] args)

{

int i,s=0;

for(i=1;i<=100;i++)

{

if(i%2!=0)

s=s+i;

}

tln("1到100的奇数累加和是"+s);

}}

3、编写程序,计算1~100中偶数的累加和并输出。

public class a

{

public static void main(String[] args)

{

int i,s=0;

for(i=1;i<=100;i++)

{

if(i%2==0)

s=s+i;

}

tln("1到100的偶数累加和是"+s);

}}

4、编写程序,计算10的阶乘并输出运算结果。

public class a

{

public static void main(String[] args)

{

int i,s=1;

for(i=1;i<=10;i++)

{

s=s*i;

}

tln("10的阶乘是"+s);

}}

5、编写程序,计算1、2、3...的累加和,条件是和小于50。

public class a

{

public static void main(String[] args)

{

int i=1,s=0;

label:

while(true)

{? s=s+i;

i++;

if(s>50)

{ s=s+1-i;

break;}}

tln("从1开始的累加和小于50的累加和是"+s);

}}

6、编写程序,计算偶数2、4、6...的累加和,条件是和小于50。

public class a

{

public static void main(String[] args)

{

int i=1,s=0;

label:

while(true)

{? s=s+2*i;

i++;

if(s>50)

{ s=s-2*i+2*1;

break;}}

tln("从2开始的偶数累加和小于50的.累加和是"+s);

}}

7、编写程序,输出下列图案:

*

***

*****

*******

public class a

{???????? public static void main(String[] args)

{ int i,k;

for(i=1;i<=4;i++)

{

for(k=1;k<=2*i-1;k++)

t("*");

tln();

}

}

}

8、编写程序,输出下列图案:

*

***

*****

*******

public class a

{???????? public static void main(String[] args)

{ int i,j,k;

for(i=1;i<=4;i++)

{

for(j=1;j<=8-2*i;j++)

t(" ");

for(k=1;k<=2*i-1;k++)

t("*");

tln();

}

}

}

9、编写程序,输出下列图案:

*******

*****

***

*

public class a

{???????? public static void main(String[] args)

{ int i,j,k;

for(i=1;i<=4;i++)

{

for(j=1;j<=2*i-2;j++)

t(" ");

for(k=1;k<=9-2*i;k++)

t("*");

tln();

}

}

}

10、编写程序在终端打印1~100之间的素数。

public class a

{???????? public static void main(String[] args)

{ int i,j;

label:

for(i=2;i<=100;i++)

{????? for(j=2;jif(i%j==0)

continue label;

t(+i);

tln();

}

}

}

11、编写一个java程序,用穷举法找出2~50之间的素数,并打印出来。

public class s{

public static void main(String args[]){

int i,j,k ;

boolean flag ;

for (i=2;i<=50 ;i++ ){

flag =true ;

k=i/2 ;

for (j=2;j<=k ;j++ ){

if (i%j==0){

flag = false ;

break ;

}

}

if (flag){

tln(i+"") ;

}

}

}

}

12、编写一自定义方法,找出两个数中的最大数,并main方法中验证。

public class a

{

static double Max(double x,double y)

{?? double t;

if(x>=y)

{

t=x;

}else

{ t=y;???? }

return t;

}

public static void main(String[] args)

{

double x,y,m;

x=549.265;

y=56.28;

m =Max(x,y);

tln("最大数是"+m);

tln("x="+x+"y="+y);

if(m>=x&&y<=m)

{

tln("ture");

}

else

{

tln("flase");

}

}

}

13、编写一自定义方法,找出两个数中的最小数,并main方法中验证。

public class a

{

static double Min(double x,double y)

{?? double t;

if(x<=y)

{

t=x;

}else

{ t=y;???? }

return t;

}

public static void main(String[] args)

{

double x,y,m;

x=245.38;

y=525.63;

m =Min(x,y);

tln("最小数是"+m);

tln("x="+x+"y="+y);

if(m<=x&&y>=m)

{

tln("ture");

}

else

{

tln("flase");

}

}

}

14、编程,找出长度为10的数组中,数组元素的最大值,并输出。

public class a

{

public static void main(String[] args)

{

double x[]={25.3,56.3,15.3,125.25,465.36,456.32,458.21,456.325,4856.3215,41.6};

double m= x[0];

int i;

for(i=0;i<10;i++)

{ if (m<=x[i])

m=x[i];

}

tln("最大数是"+m); }}

15、编程,找出长度为10的数组中,数组元素的最小值,并输出。

public class a

{

public static void main(String[] args)

{

double x[]={25.3,56.3,15.3,125.25,465.36,456.32,458.21,456.325,4856.3215,41.6};

double m=x[0];

int i;

for(i=0;i<10;i++)

{ if (m>=x[i])

m=x[i];

}

tln("最小数是"+m); }}

16、编程,找出长度为10的数组中,数组元素的最大值和最小值,并输出。

public class a

{

public static void main(String[] args)

{

double x[]={25.3,56.3,15.3,125.25,465.36,456.32,458.21,456.325,4856.3215,41.6};