Java原书第8版(第二章练习)

来源:互联网 发布:linux脚本读取文件 编辑:程序博客网 时间:2024/06/03 22:38
package Chapter1;


import java.util.Scanner;


public class Q2Test {
public static void main(String[] args) {
// 2-1
// 不能用作标识符的有:a++ --a 4#R #44 class public int
int applet, Applet, $4, apps, x, y, radius;


// 2-2
double miles = 100;
final double KILOMETERS_PER_MILE = 1.609;
double kilometers = KILOMETERS_PER_MILE * miles;
JOptionPane.showConfirmDialog(null, kilometers);


// 2-3
final int SIZE = 20;


// 2-4
int a = 1;
double d = 1.0;
// a = 46 / 9;//输出5
// a = 46 % 9 + 4 * 4 -2; //输出15
// a = 45 + 43 % 5 * (23 * 3 % 2);//输出48
// a %= 3 / a + 3;//输出1
// d = 4 + d * d + 4;//输出9.0
// d += 1.5 * 3 + (++a);//输出7.5
d -= 1.5 * 3 + a++;// 输出-4.5
System.out.println(a);
System.out.println(d);


// 2-5
System.out.println("56 % 6 = " + (56 % 6) + "\t 78 % -4 = " + (78 % -4)
+ "\t -34 % 5 = " + (-34 % 5) + "\t -34 % -5 = " + (-34 % -5)
+ "\t 5 % 1 = " + (5 % 1) + "\t 1 % 5 = " + (1 % 5));
// 输出结果是 56%6=2 78%-4=2 -34%5=-4 -34%-5=-4 5%1=0
// 1 % 5 = 1


// 2-6如果今天是星期二 ,那么100天之后是星期几
System.out.println(102 % 7);// 输出4


// 2-7输出byte int double float 的最大最小值
System.out.println("Byte\t最大值" + java.lang.Byte.MAX_VALUE + "\t最小值"
+ java.lang.Byte.MIN_VALUE + "\t空间" + java.lang.Byte.SIZE);
System.out.println("Integer\t" + java.lang.Integer.MAX_VALUE + "\t"
+ java.lang.Integer.MIN_VALUE + "\t" + java.lang.Integer.SIZE);
System.out.println("Short\t" + java.lang.Short.MAX_VALUE + "\t"
+ java.lang.Short.MIN_VALUE + "\t" + java.lang.Short.SIZE);
System.out.println("Long\t" + java.lang.Long.MAX_VALUE + "\t"
+ java.lang.Long.MIN_VALUE + "\t" + java.lang.Long.SIZE);
System.out.println("Double\t" + java.lang.Double.MAX_VALUE + "\t"
+ java.lang.Double.MIN_VALUE + "\t" + java.lang.Double.SIZE);


// 2-8除法
System.out.println(25 / 8);
System.out.println(25 / 8.0);
System.out.println("3.0*2/4结果是:" + 3.0 * 2 / 4);// 1.5
// int r=1,a1=1,b=1,c=1,d1=1;
// double result = 4 / (3 *(r + 34)) - 9 * (a1 + b * c) + (3 + d1 * (2 +
// a))/(a + b * d1);


int m = 1, r = 2;
float result = (float) (m * Math.pow(r, 2));// 输出4.0
System.out.println(result);


// int x=y=x=0;//2-12(4)非法
System.out.println((int) 12.5f);// 输出12
// 1的统一码是49,a的统一码是97,90对应的字符是z
System.out.println((int) '1' + "\t" + (int) ('1' + '2') + "\t"
+ (int) 'a' + "\t" + (char) 90);


int x1 = 'a', y1 = 'c';
System.out.println(++x1);// 98
System.out.println(y1++);// 99
System.out.println(x1 - y1);// -2 就是98-100
// 1的统一码是49
System.out.println("1" + 1);// 输出11
System.out.println('1' + 1);// 输出50
System.out.println("1" + 1 + 1);// 输出111
System.out.println("1" + (1 + 1));// 输出12
System.out.println('1' + 1 + 1);// 输出51


System.out.println(1 + "welcome" + 1 + 1);// 输出1welcome11
System.out.println(1 + "welcome" + (1 + 1));// 输出1welcome2
System.out.println(1 + "welcome" + ('\u0001' + 1));// 输出1welcome2
System.out.println(1 + "welcome" + 'a' + 1);// 输出1welcomea1


JOptionPane.showInputDialog("Enter an int number");
Integer.parseInt("10");
Double.parseDouble("12.3");


// 将摄氏温度转换为华氏温度
System.out.print("Enter a degree in Celsius :");
Scanner input = new Scanner(System.in);
double celsiusDegree = input.nextDouble();
double fahrenheit = (9.0 / 5) * celsiusDegree + 32;
System.out.println(celsiusDegree + "Celsius is " + fahrenheit
+ "Fahrenheit");
// 通过半径和高来求面积和体积
System.out.print("Enter the radius and length of cylinder :");
Scanner input1 = new Scanner(System.in);
double radius1 = input1.nextDouble();
double length = input1.nextDouble();
double area = Math.PI * radius1 * radius1;
double volume = area * length;
System.out.println("The area is :" + area);
System.out.println("The volume is :" + volume);


}
}
0 0
原创粉丝点击