java考试模拟试题

时间:2020-08-18 12:22:45 JAVA认证 我要投稿

2017年java考试模拟试题

  Sun 公司在推出 Java 之际就将其作为一种开放的技术。全球数以万计的 Java 开发公司被要求所设计的 Java 软件必须相互兼容。下面是小编整理的关于java考试模拟试题,希望大家认真阅读!

2017年java考试模拟试题

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

  x? (x>=0)

  y=

  -x? (x<0)

  import java.io.*;

  public class testa

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

  {

  float? x,y;

  InputStreamReader reader=new InputStreamReader(System.in);

  BufferedReader input=new BufferedReader(reader);

  System.out.println("请输入x:");

  String temp=input.readLine();

  x = Float.parseFloat(temp);

  if(x>=0)

  {

  y=x;

  }else

  {

  y=-x;

  }

  System.out.println("y="+y);

  }

  }

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

  import java.io.*;

  public class testa

  {

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

  {

  int x;

  InputStreamReader reader=new InputStreamReader(System.in);

  BufferedReader input=new BufferedReader(reader);

  System.out.println("请输入x:");

  String temp=input.readLine();

  x = Float.parseFloat(temp);

  if(x>=18)

  {

  System.out.println("成年人");

  }

  if(x<18)

  {

  System.out.println("未成年");

  }

  }

  }

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

  public class test

  {

  public static void main(String[] args)

  {

  int x;

  x=2020;

  if(x%2==0)

  System.out.println(+x+"是偶数");

  else

  System.out.println(+x+"不是偶数");

  }

  }

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

  public class a

  {

  public static void main(String[] args)

  {

  int i,s=1;

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

  {

  s=s*i;

  }

  System.out.println("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;}}

  System.out.println("从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;}}

  System.out.println("从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++)

  System.out.print("*");

  System.out.println();

  }

  }

  }

  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++)

  System.out.print(" ");

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

  System.out.print("*");

  System.out.println();

  }

  }

  }

  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++)

  System.out.print(" ");

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

  System.out.print("*");

  System.out.println();

  }

  }

  }

  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;

  System.out.print(+i);

  System.out.println();

  }

  }

  }

  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){

  System.out.println(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);

  System.out.println("最大数是"+m);

  System.out.println("x="+x+"y="+y);

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

  {

  System.out.println("ture");

  }

  else

  {

  System.out.println("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);

  System.out.println("最小数是"+m);

  System.out.println("x="+x+"y="+y);

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

  {

  System.out.println("ture");

  }

  else

  {

  System.out.println("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];

  }

  System.out.println("最大数是"+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];

  }

  System.out.println("最小数是"+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};