當前位置:才華齋>計算機>計算機二級>

2017年9月計算機二級考試JAVA考試題及答案

計算機二級 閱讀(3.12W)

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};