1、仔细阅读示例: EnumTest.java,运行它,分析运行结果? 你能得到什么结论?你掌握了枚举类型的基本用法了吗?
package text;
public class Enum {public static void main(String[] args) {
Size s=Size.SMALL;
Size t=Size.LARGE;
//s和t引用同一个对象?
System.out.println(s==t); //
//是原始数据类型吗?
System.out.println(s.getClass().isPrimitive());
//从字符串中转换
Size u=Size.valueOf("SMALL");
System.out.println(s==u); //true
//列出它的所有值
for(Size value:Size.values()){
System.out.println(value);
}
}
}
enum Size{SMALL,MEDIUM,LARGE};
运行结果:
false
false
true
SMALL
MEDIUM
LARGE
结论:枚举可用于switch语句中。枚举不属于原始数据类型,它的每个具体值都引用一个特定的对象。相同的值则引用同一个对象。
可以使用“==”和equals()方法直接比对枚举变量的值,换句话说,对于枚举类型的变量,“==”和equals()方法执行的结果是等价的。
2、
使用两个输入框输入数据 用一个消息框显示计算结果
package text;
//An addition program
import javax.swing.JOptionPane; // import class JOptionPane
public class Addition {
public static void main( String args[] )
{
String firstNumber, // first string entered by user
secondNumber; // second string entered by user
int number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2
// read in first number from user as a string
firstNumber =
JOptionPane.showInputDialog( "Enter first integer" );
// read in second number from user as a string
secondNumber =
JOptionPane.showInputDialog( "Enter second integer" );
// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
// add the numbers
sum = number1 + number2;
// display the results
JOptionPane.showMessageDialog(
null, "The sum is " + sum, "Results",
JOptionPane.PLAIN_MESSAGE );
System.exit( 0 ); // terminate the program
}
}
运行结果:Enter first integer 4
Enter second integer 4
The sum is 9
3、请运行以下代码
package text;
public class Textdouble {
public static void main(String args[]) {
System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
System.out.println("4.015 * 100 = " + (4.015 * 100));
System.out.println("123.3 / 100 = " + (123.3 / 100));
}
}
运行结果:
0.05 + 0.01 = 0.060000000000000005
1.0 - 0.42 = 0.5800000000000001
4.015 * 100 = 401.49999999999994
123.3 / 100 = 1.2329999999999999
使用double类型的数值进行计算, 其结果是不精确的。float的符号位占一位,阶码为8位,尾数为23位,所以一共四个字节;double的符号位占一位,阶码为11位,尾数52位。符号位为0的话说明是正的,为1的话说明是负的;阶码就是尾数所要乘以的2的次方数;尾数默认是去掉整数位的1的,也就是说加入尾数序列为01010101(后面还有很多),那么其实它是1.01010101(后面还有很多)。比如一个float类型的变量,其符号位为0,阶码化成十进制为3,尾数为01000000(后面都是0)。
4、
以下代码的输出结果是什么?
int X=100;
int Y=200;
System.out.println("X+Y="+X+Y);
System.out.println(X+Y+"=X+Y");
运行结果:X+Y=100200 300=X+Y
在语句一中,“+”是连接量;在语句二中,“+”是运算符,进行求和运算。
5、
package text;
import java.util.Scanner;
public class InputTest {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// get first input
System.out.print("What is your name? ");
String name = in.nextLine();
// get second input
System.out.print("How old are you? ");
int age = in.nextInt();
/* int i;
String value="100";
i=Integer.parseInt(value);
i=200;
String s=String.valueOf(i);*/
// display output on console
System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
}
}
运行结果:
What is your name? hh
How old are you? 20
Hello, hh. Next year, you'll be 21