JAVA課後作業

真的不会qiao代码發表於2024-09-27

1.列舉值的foreach迭代:
public class Enum {
private enum MyEnum{
ONE,TWO,THREE
}
public static void main(String [] args) {
for(MyEnum value:MyEnum.values()) {
System.out.println(value);
}
}
}
這種foreach列舉迭代,無需知道具體列舉的數量,直接就可以按序列舉

2.兩數相加的對話方塊展示
import javax.swing.JOptionPane; // import class JOptionPane

public class Enum {
// An addition program
public static void main( String args[] )
{
String firstNumberString,secondNumberString;
int number1,number2;

	   firstNumberString = JOptionPane.showInputDialog("Enter first integer");
	   
	   secondNumberString = JOptionPane.showInputDialog("Enter second integer");
	   //字串型別轉化為整型
	   number1 = Integer.parseInt(firstNumberString);
	   number2 = Integer.parseInt(secondNumberString);
	   
	   int sum = number1+number2;
	   
	   JOptionPane.showMessageDialog(null, "This sum is "+ sum ,"Rssult",JOptionPane.PLAIN_MESSAGE);
	   
	   System.exit(0);
   }

}
第一次瞭解了JOptionPane這種在JAVA可以展示對話方塊的類,還有Integer.parseInt這種將字串轉化成整型的方法

3.精度損失測試

public class Enum {
public static void main(String[] args) {
double number1 = 123456789;
float number2 = (float) number1;
System.out.println(number1);
System.out.println(number2);
}
}在double型別強轉成float型別時出現了精度損失,第一次的輸出為1.23456789E8
第二次的輸出為1.23456792E8
同理那個Int Long long型別轉化成float和double型別的時候都會出現精度損失的問題

double數值進行計算時出現問題
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
我知道在計算機中所有的資料都是以二進位制的方式儲存的,所有的浮點型資料都是近似的值,所以
在小數類的計算時,精度是無法保證的

改進精度,使用BigDecimal類
import java.math.BigDecimal;
public class Enum {
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));
}
}
在構建物件時使用String型別原因是,使用String型別可以精確的表示十進位制的任何數字,而不會出現精度的損失,如果使用
double資料型別,則在建立BigDeCimal時穿進去的是一個double型別的近似值

字串連結的相關注意:
public class Enum {
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");
}
}
二者輸出不同,第一個輸出是加了兩個字串,第二個是先計算二者的相加值,在加一個字串,第一個相當於三個字串,第二個相當於兩個

  1. 隨機數的生成
    import java.util.Random;

public class Enum {
public static void main(String[] args)
{
Random random = new Random();
for(int i = 0;i < 30;i++) {
int number1 = random.nextInt(11)+1;
int number2 = random.nextInt(11)+1;
int middle = random.nextInt(4);
switch(middle) {
case 0:
System.out.println(number1+" + "+number2+" =");
break;
case 1:
System.out.println(number1+" - "+number2+" =");
break;
case 2:
System.out.println(number1+" * "+number2+" =");
break;
case 3:
if(number2==0) {
i--;
continue;
}else {
System.out.println(number1+" / "+number2+" =");
}
}
}
}
}

相關文章