1、package my.p2;import java.util.Scanner;public class Practice public static void main(String args) / 2.14ASystem.out.println(“习题 2.14 答案“);System.out.println(“1 2 3 4“);/ 2.14BSystem.out.print(“1 “);System.out.print(“2 “);System.out.print(“3 “);System.out.print(“4“);/ 2.14CSystem.out.printf(“n%d %d %
2、d %d“, 1, 2, 3, 4); / % 格式说明符,后接数据类型/ 2.15:import firstSystem.out.print(“n 习题 2.15 答案(提示:类似 SelfTest2_5.java)“);Scanner input = new Scanner(System.in);int n1, n2, products, quotients, sums, differences;System.out.print(“ninput the first integer:n“);n1 = input.nextInt();System.out.print(“input the se
3、cond integer:n“);n2 = input.nextInt();sums = n1 + n2;differences = n1 - n2;products = n1 * n2;quotients = n1 / n2;System.out.printf(“the four operations(+ - /) outcomes are %d %d %d %d“, sums,differences, products, quotients);/ 2.16System.out.print(“nn 习题 2.16 答案n“);System.out.printf(“The numbers ar
4、e %d and %d“, n1, n2);if (differences 0)System.out.printf(“nnThe larger is n1= %dn“, n1);if (differences n3)min = n3;if (n3 n1)max = n3;System.out.printf(“nThe maximum number is %d , and the minimum number is %d“,max, min);/ 2.18System.out.print(“n 习题 2.18 答案n“);System.out.print(“nthe quadrate is n“
5、);System.out.printf(“*n* *n* *n* *n* *n* *n* *n* *n* *n*n“);System.out.print(“nthe ellipse is n“);System.out.printf(“ * n * * n* *n* *n* *n* *n* *n* *n * * n * n“);System.out.print(“nthe arrow is n“);System.out.printf(“ * n * n * n * n * n * n * n * n * n * n“);System.out.print(“nthe rhombus is n“);
6、System.out.printf(“ * n * n * * n * * n* *n * * n * * n * n * n“);/ pentagam/ System.out.print(n)/ 2.19System.out.print(“n 习题 2.19 答案n“);System.out.println(“*n*n*n*n*“);/ 2.20-23 略,2.24 略/ 2.25System.out.print(“n 习题 2.25 答案n“);int n4, testn;System.out.print(“n 输入一个 5 位整数n“);n4 = input.nextInt();test
7、n = n4 % 2;if (testn = 0)System.out.printf(“n%d is 偶数n“, n4);if (testn != 0)System.out.printf(“n%d is 奇数n“, n4);/ 2.26System.out.print(“n 习题 2.26 答案n“);int testnn;testnn = n1 % n2;if (testnn = 0)System.out.printf(“n%d is %d 的倍数n“, n1, n2);if (testnn != 0)System.out.printf(“n%d isnt %d 的倍数n“, n1, n2)
8、;/ 2.28System.out.print(“n 习题 2.26 答案n“);int r = n3;double di, ci, ar;di = 2 * r;ci = 2 * Math.PI * r;ar = Math.PI * r * r;System.out.printf(“nThe diameter of the circle is %f;nthe perimeter of the circle is %f;nand the area of the circle is %fn“,di, ci, ar); / %f 不能写成%d/ 2.29System.out.print(“n 习题
9、2.29 测试n“);System.out.printf(“The character of %c has the value %dn“, A,(int) A);System.out.printf(“The character of %c has the value %dn“, B,(int) B);System.out.printf(“The character of %c has the value %dn“, C,(int) C);System.out.printf(“The character of %c has the value %dn“, a,(int) a);System.ou
10、t.printf(“The character of %c has the value %dn“, b,(int) b);System.out.printf(“The character of %c has the value %dn“, c,(int) c);System.out.printf(“The character of %c has the value %dn“, 0,(int) 0);System.out.printf(“The character of %c has the value %dn“, 1,(int) 1);System.out.printf(“The charac
11、ter of %c has the value %dn“, 2,(int) 2);System.out.printf(“The character of %c has the value %dn“, $,(int) $);System.out.printf(“The character of %c has the value %dn“, *,(int) *);System.out.printf(“The character of %c has the value %dn“, +,(int) +);System.out.printf(“The character of %c has the va
12、lue %dn“, /,(int) /);/ 2.30System.out.print(“n 习题 2.30 答案n“);System.out.print(“n 查看 n4n“);System.out.printf(“The character of %d divided into the value %d %d %d %d %d“,n4, (n4 - n4 % 10000) / 10000),(n4 % 10000 - n4 % 10000 % 1000) / 1000),(n4 % 1000 - n4 % 100) / 100,(n4 % 100 - n4 % 10) / 10, n4 %
13、 10);/2.31System.out.print(“n 习题 2.30 答案n“);System.out.printf(“%st%st%sn“,“number“,“square“,“cube“);System.out.printf(“%st%st%sn“,0,0,0);System.out.printf(“%st%st%sn“,1,1,1);System.out.printf(“%st%st%sn“,2,4,8);System.out.printf(“%st%st%sn“,3,9,27);System.out.printf(“%st%st%sn“,4,16,64);System.out.printf(“%st%st%sn“,5,25,125);System.out.printf(“%st%st%sn“,6,36,216);System.out.printf(“%st%st%sn“,7,49,343);System.out.printf(“%st%st%sn“,8,64,512);System.out.printf(“%st%st%sn“,9,81,729);System.out.printf(“%st%st%sn“,10,100,1000);