guozi6

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

问题一:

源代码:

public class EnumTest {

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};

 

运行结果:

 

 结论:

枚举类型是引用类型,枚举不属于原始数据类型,它的每个具体值都引用一个特定的对象。相同的值则引用同一个对象。可以使用“==”和equals()方法直接比对枚举变量的值,换句话说,对于枚举类型的变量,“==”和equals()方法执行的结果是等价的。

 

问题二:

源代码:


// 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
}
}
运行结果:

 

 

 问题三:

源代码:

package homework;

public class text01 {
private static int value=1;

public static void main(String[] args) {
int value=2;
System.out.println(value);
}
}
运行结果:
输出了2
结论:
说明,在定义一个变量的时候,在里面的定义赋值,优先级更高,但是作用域仅仅只在主函数里

问题四:

源代码:

public class TestDouble {

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));
}
}

运行结果:

 

结论:

 

很意外,没想到double的数值去运行计算,结果都是不准确的,虽然影响小点,double类型的数值占用64bit,即64个二进制数,除去最高位表示正负符号的位,在最低位上一定会与实际数据存在误差(除非实际数据恰好是2的n次方)。

问题五:

源代码:


import java.math.BigDecimal;

public class TestBigDecimal
{
public static void main(String[] args)
{
BigDecimal f1 = new BigDecimal("0.05");
BigDecimal f2 = BigDecimal.valueOf(0.01);
BigDecimal f3 = new BigDecimal(0.05);
System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
System.out.println("0.05 + 0.01 = " + f1.add(f2));
System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
System.out.println("0.05 / 0.01 = " + f1.divide(f2));
System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
System.out.println("0.05 + 0.01 = " + f3.add(f2));
System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
System.out.println("0.05 / 0.01 = " + f3.divide(f2));
}
}

运行结果:

 结论:

double不能准确地代表16位有效数以上的数字,在使用BigDecimal时,应用 BigDecimal(String)构造器创建对象才有意义。另外,BigDecimal所创建的是对象,我们不能使用传统的+、-、*、/等算术运算 符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象。

问题六:

源代码:

package homework;

public class text01 {

public static void main(String[] args) {
int x=100;
int y=200;
System.out.println("x+y="+x+y);
System.out.println(x+y+"x+y");
}
}

posted on 2023-09-15 11:58  汀幻  阅读(1)  评论(0编辑  收藏  举报