java程序设计基础_陈国君版第五版_第四章习题
import java.util.Scanner;
public class Main4_1 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
float testScore ;
char grade ;
do{
System.out.println("请输入成绩:");
testScore = scanner.nextFloat();
}while((testScore<0)||(testScore>100));
switch((int)testScore/10){
case 10 :
case 9 : grade = 'A'; break;
case 8 : grade = 'B'; break;
case 7 : grade = 'C'; break;
case 6 : grade = 'D'; break;
default: grade = 'E'; break;
}
System.out.println("本次成绩:"+testScore+"\t获得的等级是:"+grade);
}
}
import java.util.Scanner;public class Main4_2{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.print("请输入学生的成绩:"); float testScore = scanner.nextFloat(); char grade; switch((int)testScore/10){ case 10: case 9 : grade = 'A';break; case 8 : if(testScore>=85){grade = 'A';break;} else{grade = 'B';break;} case 7 : grade = 'B';break; case 6 : grade = 'C';break; default: grade = 'D';break; } System.out.println("成绩"+testScore+"的等级是: "+grade); }}
public class Main4_3{ public static void main(String[] args){ for(int i = 1;i<100;i++){ if(i%21==0){ System.out.println(i+"\t"); } } }}
import java.io.*;public class Main4_4{ public static void main(String[] args)throws IOException{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入一个数:"); String str = buf.readLine(); int n = Integer.parseInt(str); int sum = 0 ; for(int i = 1;i<=n;i++){ int s = 1; for(int j = 1 ; j<=i;j++){ s = s*j; } sum += s; } System.out.println("结果为:"+sum); }}
import java.util.Scanner;public class Main4_5{ public static void main(String[] args){ float sum = 0; Scanner scanner = new Scanner(System.in); System.out.println("请输入一个数 n:"); int n = scanner.nextInt(); for(int i = 1;i<=n;i++){ int s =1; for(int j = 1;j <= i ; j++){ s = s*j; } sum += Math.pow(-1,i-1)/(float)s; } System.out.println("sum = "+sum); }}
public class Main4_6{ public static void main(String[] args){ int flag = 0; for(int i = 100; i < 1000; i++){ int num1 = i%10; int num2 = (i%100)/10; int num3 = i/100; if(i==(Math.pow(num1,3)+Math.pow(num2,3)+Math.pow(num3,3))){ System.out.print(i+"\t"); flag++; } if(flag >10){ System.out.println(); flag = 0; } } }}
import java.io.*;public class Main4_7{ public static void main(String[] args)throws IOException{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); String str ; System.out.println("请输入一个数:"); str = buf.readLine(); int n = Integer.parseInt(str); int sum = 0; for(int i = 1 ;i < n ; i++){ if(n%i==0){ sum += i; } } if(sum == n){ System.out.println(n+"是一个完全数!"); }else System.out.println(n+"不是一个完全数!"); }}
import java.io.*;public class Main4_8{ public static void main(String[] args)throws IOException{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入一个整数:"); String str = buf.readLine(); int n = Integer.parseInt(str); System.out.print(n+"的各位数字之和为"); int flag =1; while((n/(int)(Math.pow(10,flag)))!=0){ flag++; } while(flag!=0){ System.out.print((int)(n/(Math.pow(10,flag-1)))); n %= Math.pow(10,flag-1); flag--; if(flag!=0){ System.out.print("+"); } } }}
import java.util.Scanner;public class Main4_9{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("请输入一个浮点型数:"); float n = scanner.nextFloat(); System.out.println(n+"的整数部分为"+(int)n); System.out.println(n+"的小数部分为"+(n-(int)n)); }}
public class Main4_10{ public static void main(String[] args){ float length = 3000; int num = 0; do{ length /= 2; num++; }while(length>=5); System.out.println("需要"+num+"天,绳子的长度会短于5米"); }}
public class Main4_11{ public static void main(String[] args){ int n = 5,i=0,j,z; int [][] a = new int [n][n]; a[0][0] = 1; for(z = 1 ; z<n;z++){ //z判断有多少行 j=0; i = z; //哪行开始 a[i][j]=a[j][i-1]+1; do{ //斜向上走 赋值 a[i-1][j+1]=a[i][j]+1; i--; j++; }while(j!=z); } for(i=0;i<n;i++){ for(j=0;j<(5-i);j++) System.out.print(a[i][j]+"\t"); System.out.println(); } }}