希望大家给予批评指正,在下感激不尽(未完,待续……)
第三章
练习9 (1)分别显示用float和double指数记数法所能表示的最大和最小数字
public class MaxMinFloatDouble {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
float fmax = Float.MAX_VALUE;
float fmin = Float.MIN_VALUE;
double dmax = Double.MAX_VALUE;
double dmin = Double.MIN_VALUE;
System.out.println(fmax);
System.out.println(fmin);
System.out.println(dmax);
System.out.println(dmin);
/*result:
3.4028235E38
1.4E-45
1.7976931348623157E308
4.9E-324*/
}
}
练习(10)编写一个具有两个常量值的程序,一个具有交替的二进制位1和0,其中最低有效位为0,另一个也具有交替的二进制位1和0,但其最低有效位为1(提示:使用十六进制常量来表示最简单的方法).取两个值,用按位操作符以所有可能方式结合运算它们,然后用Integer.toBinaryString()显示
public static void main(String[] args) {
int i1 = 0xaaaaaaaa;
int i2 = 0x55555555;
System.out.println(Integer.toBinaryString(i1));
System.out.println(Integer.toBinaryString(i2));
/**
* result
* 10101010101010101010101010101010
1010101010101010101010101010101
*/
}
练习14 (3)编写一个接收两个字符串参数的方法,用各种布尔值的比较关系来比较这两个字符串,然后把结果打印出来。做==和!=的同时,用equals()作测试。在main()里面用几个不同的字符串对象调用这个方法。
/**
* @param args
*/
public static void main(String[] args) {
String str1="hello";
String str2="word";
String str3=new String("hello");
testStr(str1, str2);
System.out.println("--------------------------");
testStr(str1, str3);
}
public static void testStr(String s1,String s2){
//boolean b1=s1>s2;
//boolean b2=s1<s2;
boolean b3=s1==s2;
System.out.println(s1+"=="+s2+":\t"+b3);
boolean b4=s1.equals(s2);
System.out.println(s1+".equals("+s2+"):\t"+b4);
boolean b5=s1!=s2;
System.out.println(s1+"!="+s2+":\t"+b5);
/**result
*
* hello==word: false
hello.equals(word): false
hello!=word: true
--------------------------
hello==hello: false
hello.equals(hello): true
hello!=hello: true
*/
}