久久九九国产无码高清_人人做人人澡人人人爽_日本一区二区三区中文字幕_日韩无码性爱免费

JAVA模擬試題及答案

時間:2024-10-18 17:28:38 SUN認證 我要投稿
  • 相關推薦

JAVA模擬試題及答案

  Sun 公司在推出 Java 之際就將其作為一種開放的技術(shù)。那么java作為一種編程語言,你對java編程題有把握嗎?下面跟yjbys小編一起來看看吧!

JAVA模擬試題及答案

  【程序1】

  題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數(shù)為多少?

  這是一個菲波拉契數(shù)列問題

  public class lianxi01 {

  public static void main(String[] args) {

  System.out.println("第1個月的兔子對數(shù): 1");

  System.out.println("第2個月的兔子對數(shù): 1");

  int f1 = 1, f2 = 1, f, M=24;

  for(int i=3; i<=M; i++) {

  f = f2;

  f2 = f1 + f2;

  f1 = f;

  System.out.println("第" + i +"個月的兔子對數(shù): "+f2);

  }

  }

  }

  【程序2】

  題目:判斷101-200之間有多少個素數(shù),并輸出所有素數(shù)。

  程序分析:判斷素數(shù)的方法:用一個數(shù)分別去除2到sqrt(這個數(shù)),如果能被整除, 則表明此數(shù)不是素數(shù),反之是素數(shù)。

  public class lianxi02 {

  public static void main(String[] args) {

  int count = 0;

  for(int i=101; i<200; i+=2) {

  boolean b = false;

  for(int j=2; j<=Math.sqrt(i); j++)

  {

  if(i % j == 0) { b = false; break; }

  else { b = true; }

  }

  if(b == true) {count ++;System.out.println(i );}

  }

  System.out.println( "素數(shù)個數(shù)是: " + count);

  }

  }

  【程序3】

  題目:打印出所有的 "水仙花數(shù) ",所謂 "水仙花數(shù) "是指一個三位數(shù),其各位數(shù)字立方和等于該數(shù)本身。例如:153是一個 "水仙花數(shù) ",因為153=1的三次方+5的三次方+3的三次方。

  public class lianxi03 {

  public static void main(String[] args) {

  int b1, b2, b3;

  for(int m=101; m<1000; m++) {

  b3 = m / 100;

  b2 = m % 100 / 10;

  b1 = m % 10;

  if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m) {

  System.out.println(m+"是一個水仙花數(shù)"); }

  }

  }

  }

  【程序4】

  題目:利用條件運算符的嵌套來完成此題:學習成績> =90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。

  import java.util.*;

  public class lianxi05 {

  public static void main(String[] args) {

  int x;

  char grade;

  Scanner s = new Scanner(System.in);

  System.out.print( "請輸入一個成績: ");

  x = s.nextInt();

  grade = x >= 90 ? ’A’

  : x >= 60 ? ’B’

  :’C’;

  System.out.println("等級為:"+grade);

  }

  }

  【程序5】

  題目:輸入兩個正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)。

  /**在循環(huán)中,只要除數(shù)不等于0,用較大數(shù)除以較小的數(shù),將小的一個數(shù)作為下一輪循環(huán)的大數(shù),取得的余數(shù)作為下一輪循環(huán)的較小的數(shù),如此循環(huán)直到較小的數(shù)的值為0,返回較大的數(shù),此數(shù)即為最大公約數(shù),最小公倍數(shù)為兩數(shù)之積除以最大公約數(shù)。* /

  import java.util.*;

  public class lianxi06 {

  public static void main(String[] args) {

  int a ,b,m;

  Scanner s = new Scanner(System.in);

  System.out.print( "鍵入一個整數(shù): ");

  a = s.nextInt();

  System.out.print( "再鍵入一個整數(shù): ");

  b = s.nextInt();

  deff cd = new deff();

  m = cd.deff(a,b);

  int n = a * b / m;

  System.out.println("最大公約數(shù): " + m);

  System.out.println("最小公倍數(shù): " + n);

  }

  }

  class deff{

  public int deff(int x, int y) {

  int t;

  if(x < y) {

  t = x;

  x = y;

  y = t;

  }

  while(y != 0) {

  if(x == y) return x;

  else {

  int k = x % y;

  x = y;

  y = k;

  }

  }

  return x;

  }

  }

  【程序6】

  題目:輸入一行字符,分別統(tǒng)計出其中英文字母、空格、數(shù)字和其它字符的個數(shù)。

  import java.util.*;

  public class lianxi07 {

  public static void main(String[] args) {

  int digital = 0;

  int character = 0;

  int other = 0;

  int blank = 0;

  char[] ch = null;

  Scanner sc = new Scanner(System.in);

  String s = sc.nextLine();

  ch = s.toCharArray();

  for(int i=0; i

  if(ch >= '0' && ch <= '9') {

  digital ++;

  } else if((ch >= 'a' && ch <= 'z') || ch > 'A' && ch <= 'Z') {

  character ++;

  } else if(ch == ' ') {

  blank ++;

  } else {

  other ++;

  }

  }

  System.out.println("數(shù)字個數(shù): " + digital);

  System.out.println("英文字母個數(shù): " + character);

  System.out.println("空格個數(shù): " + blank);

  System.out.println("其他字符個數(shù):" + other );

  }

  }

【JAVA模擬試題及答案】相關文章:

Java考試格林模擬試題10-22

2017年java模擬試題06-20

java基礎筆試題及答案10-18

java面試題及答案11-01

java考試試題及答案10-25

2017年java考試模擬試題05-31

sun認證java基礎模擬試題10-26

java面試題2017及答案06-08

2017年Java筆試題及答案08-03

java認證考試試題及答案07-21