java程序读写题
1、读下列程序,写出正确的运行结果。
public class Test1 { String str1="Hello, world!t";String str2="Hello, students!";public static void main(String args[]){ System.out.print(str1);System.out.println(str2);} } 更改:
public class Test1 { public static void main(String args[]){ String str1="Hello, world!t";String str2="Hello, students!";System.out.print(str1);System.out.println(str2);} }
2、读下列程序,写出正确的运行结果。
public class Test2{ public static void main(String[] args){ int i,j;char c="*";for(i=6;i>0;i--){ for(j=0;j<=i;j++)System.out.print("* ");System.out.println();} } } 3、读下列程序,写出正确的运行结果。
public class Test3 { public static void main(String arge[]){ int i=0;for(char ch = 97;ch<109;ch++,i++){ if(i % 4 == 0)System.out.println(" ");System.out.print("t" +ch);}}} 4、读下列程序,写出正确的运行结果。
public class Test4{ static int j=0,k=0;public static void main(String args[]){
System.out.println(mB(11));System.out.println(j);} static Boolean mA(int A){ j+=A;return true;} static Boolean mB(int k){ Boolean b;b=k>10&mA(1);b=k>10&&mA(2);return b;}} 5、读下列程序,写出正确的运行结果。
public class Test5{ public static void main(String[] args){ try { method();} catch(Exception e){System.out.println("A");} finally { System.out.println("B");} } static void method(){ try { wrench();System.out.println("C");} catch(ArithmeticException e){ System.out.println("D");} finally { System.out.println("E");} System.out.println("F");} static void wrench(){ throw new NullPointerException();}}
6、读下列程序,写出正确的运行结果。
import java.io.*;public class Test6{ public static void main(String args[ ]){ int[] a={42,99,5,63,95,36,2,69,200,96};System.out.println("排序前的数据序列:");ShowArray(a);Sort(a);System.out.println("排序后的数据序列:");ShowArray(a);} public static void Sort(int[] x){ int w;for(int i=1;i 程序设计题 1、、数 输入两个正整数 m 和 和 n,求其最大公约数和最小公倍数。 import java.util.*;public class Test1 { public static void main(String[] args){ int a ,b,m;Scanner s = new Scanner(System.in);System.out.print("键入一个整数: ");a = s.nextInt();System.out.print("再键入一个整数: ");b = s.nextInt();deff cd = new deff();m = cd.deff(a,b);int n = a * b / m;System.out.println("最大公约数: " + m);System.out.println("最小公倍数: " + 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;} } 2、、出 打印出 1000 以内 所有的 " 水仙花数 ",所谓 " 水仙花数 " 是指一个三位数,其各位数字立方和等于该数本身。 public class Test2 { 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+"是一个水仙花数");} } } } 3、、第 古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? public class Test3{ public static void main(String[] args){ System.out.println("第 1 个月的兔子对数: 1");System.out.println("第 2 个月的兔子对数: 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 +"个月的兔子对数: "+f2);} } } 4、、断 判断 101-300 之间有多少个素数,并输出所有素数。 public class Test4 { 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("素数个数是: " + count);} } 5、输入 100 以内的 学习成绩,成绩> =90 分的同学用 A 表示,60-89 分之间的用 B 表示,60 分以下的用 C 表示。 import java.util.*;public class Test5 { 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); } } 6、、一个数如果恰好等于它的因子之和,这个数就称为 " 完数 "。例如 6=1 +2 +3,编程找出 出 1000 以内的所有完数。 public class Test6{ public static void main(String[] args){ System.out.println("1 到 1000 的完数有: ");for(int i=1;i<1000;i++){ int t = 0;for(int j=1;j<= i/2;j++){ if(i % j == 0){ t = t + j;} } if(t == i){ System.out.print(i + " ");} } } } 7.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 import java.util.*;public class Test7 { 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);System.out.print("请输入一行字符:"+"n");String s = sc.nextLine();ch = s.toCharArray();for(int i=0;i import java.util.*; public class Test8 { public static void main(String[] args){ Scanner s = new Scanner(System.in);int a[] = new int[10];for(int i=0;i<10;i++){ a[i]=(int)(Math.random()*100);} System.out.println("随机产生的数组为:");for(int j=0;j<10;j++){ System.out.print(a[j]+" ");} System.out.println("n 数组逆序输出为:");for(int j=9;j>=0;j=j-1){ System.out.print(a[j]+" ");} } }
