1. EnumTest.java
public class EnumTest { public static void main (String [] args) { Size S = Size.SMALL; Size t = Size.LARGE; // S and t refer to the same object? System.out.println (S == t); // // primitive data type? System.out.println (s.getClass () isPrimitive ().); // convert from a string Size Size.valueOf U = ( "the SMALL" ); System.out.println (S == U); // to true // List of all its values for (Size value: Size.values ()) { System.out.println (value); } } } enum Size{SMALL,MEDIUM,LARGE};
Different values of s and t, t == s result is false
Size is not a primitive type, returns false
Enumerated type that is equal to a variable
values () Returns all values of
Summary: Enumeration enum is a special class (or classes), using enumeration can easily define constants
A common situation is the switch statement, using the enumeration to judge
Note: Because it is constant, so are generally all uppercase
The benefits of using the case of the enumeration is that you can limit the scope of variables within a few, when using the switch would not have to worry about other values
2. TestDouble.java
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)); } }
operation result:
0.05 + 0.01 = 0.060000000000000005
1.0 - 0.42 = 0.5800000000000001
4.015 * 100 = 401.49999999999994
123.3 / 100 = 1.2329999999999999
Conclusion: Use of type double value is calculated, the result is inaccurate.
Workaround - Use the BigDecimal class
Import java.math.BigDecimal; public class TestBigDecimal { public static void main (String [] args) { the BigDecimal F1 = new new the BigDecimal ( "0.05" ); the BigDecimal F2 = BigDecimal.valueOf (0.01 ); the BigDecimal F3 = new new the BigDecimal (0.05 ); System.out.println ( "String below using the calculation result as a BigDecimal constructor arguments:" ); System.out.println ( "+ 0.01 = 0.05" + f1.add (F2)); System.out.println ( "0.05 - 0.01 =" + f1.subtract (F2)); System.out.println ( "* 0.01 = 0.05" + f1.multiply (F2)); System.out.println ( "0.05 / 0.01 =" + f1.divide (F2)); System.out.println ( "below use as double BigDecimal constructor parameter calculations: " ); System.out.println ( " + 0.01 = 0.05 "+ f3.add (F2)); System.out.println ( " 0.05 - 0.01 = "+ F3. Subtract (F2)); System.out.println ( "* 0.01 = 0.05" + f3.multiply (F2)); System.out.println ( "0.05 / 0.01 =" + f3.divide (F2)); } }
operation result:
Below using String as BigDecimal constructor parameter calculations:
0.05 + 0.01 = 0.06
0.05 - 0.01 0.04
0.05 * 0.01 = 0.0005
0.05 / 0.01 = 5
below using double calculation result BigDecimal constructor arguments:
0.05 + 0.01 = 0.06000000000000000277555756156289135105907917022705078125
0.05 - 0.04000000000000000277555756156289135105907917022705078125 0.01 =
0.05 * 0.01 = 0.0005000000000000000277555756156289135105907917022705078125
0.05 / 0.01 = 5.000000000000000277555756156289135105907917022705078125
3.
int X=100; int Y=200; System.out.println("X+Y="+X+Y); System.out.println(X+Y+"=X+Y");
Use the + operator connected literal "The sum is" a String and sum calculations, and other types of data are added, the result is a new String
4.
Automatically generating n channel entitled Primary four operations "software"
//信1805-2孙超凡20183558 import java.util.Scanner; public class Mathtest{ public static void main(String[] args) { int[] chucun1= new int [1001]; int[] chucun2= new int [1001]; int[] chucun3= new int [1001]; int[] chucun4= new int [1001]; int[] chucun5= new int [1001]; int[] chucun6= new int [1001]; int[] chucun7= new int [1001]; int[] chucun8= new int [1001]; int max=100,min=1; int ran1 = (int) (Math.random()*(max-min)+min); int ran2=(int) (Math.random()*(max-min)+min); int zhuangtai=0; char fuhao[]={'+','+','-','*','/'}; max=4;min=1; int ran3=(int) (Math.random()*(max-min)+min); int shuliang; System.out.println("enter the number1:"); Scanner scan=new Scanner(System.in); shuliang=scan.nextInt(); System.out.println("enter the number2:"); int hang=scan.nextInt(); int p=0; for(int i=1;i<shuliang+1;++i) { ++p; max=100;min=1; ran1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); max=5;min=1; ran3=(int) (Math.random()*(max-min)+min); switch(ran3){ case 1:{max=100;min=1; chucun5[i]=ran1; chucun6[i]=ran2; for(int m=0;m<i;++m){ if(ran1==chucun5[m]||ran2==chucun6[m]){ zhuangtai=1; break;} zhuangtai=0; } while(zhuangtai==1){ ran1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); chucun5[i]=ran1; chucun6[i]=ran2; for(int m=0;m<i;++m){ if(ran1==chucun5[m]||ran2==chucun6[m]){ zhuangtai=1; break;} zhuangtai=0;} } System.out.println(i+","+ran1+" + "+ran2+" ="); break; } case 2:{ max100 =; = min. 1 min);; chucun1 [I] = RAN1; chucun2 [I] = RAN2; for ( int m = 0; m <I; ++ m) { IF (RAN1 == chucun1 [m] || RAN2 == chucun2 [m]) { zhuangtai =. 1 ; BREAK ;} zhuangtai = 0 ; } the while (zhuangtai ==. 1 ) { RAN1 = ( int ) (Math.random () * (max-min) + RAN2 = (int) (Math.random()*(max-min)+min); chucun1[i]=ran1; chucun2[i]=ran2; for(int m=0;m<i;++m){ if(ran1==chucun1[m]||ran2==chucun2[m]){ zhuangtai=1; break;} zhuangtai=0;} } if(ran1>ran2)System.out.println(i+","+ran1+" - "+ran2+" ="); else System.out.println(i+","+ran2+" - "+ran1+" ="); break; } case 3: { max=100;min=1; ran1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); chucun3[i]=ran1; chucun4[i]=ran2; while((ran1*ran2)>=100){ ran1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); chucun3[i]=ran1; chucun4[i]=ran2; } for(int m=0;m<i;++m){ if(ran1==chucun3[m]||ran2==chucun4[m]){ zhuangtai=1; break;} zhuangtai=0; } while(zhuangtai==1&&(ran1*ran2)>=100){ ran1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); chucun3[i]=ran1; chucun4[i]=ran2; for(int m=0;m<i;++m){ if(ran1==chucun3[m]||ran2==chucun4[m]){ zhuangtai=1; break;} zhuangtai=0;} } System.out.println(i+","+ran1+" * "+ran2+" ="); max=100;min=1; ran1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); break; } case 4:{ max=100; if(ran1>ran2){ float ran=ran1/ran2; while(ran1%ran2!=0){ ran1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); } chucun7[i]=ran1; chucun8[i]=ran2; for(int m=0;m<i;++m){ if(ran1==chucun7[m]||ran2==chucun8 [m]) { zhuangtai =. 1 ; BREAK ; } zhuangtai = 0 ; } the while (zhuangtai ==. 1 ) { RAN1 = ( int ) (Math.random () * (max-min) + min); RAN2 = ( int ) (Math.random () * (max-min) + min); RAN = RAN1 / RAN2; the while ! (RAN1% RAN2 = 0 ) { RAN1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); } chucun7[i]=ran1; chucun8[i]=ran2; for(int m=0;m<i;++m){ if(ran1==chucun7[m]||ran2==chucun8[m]){ zhuangtai=1; break;} zhuangtai=0;} // System.out.println(zhuangtai+"!!"); } System.out.println(i+","+ran1+" / "+ran2+" ="); break; } else { int t=ran2; ran2=ran1; ran1=t; float ran=ran1/ran2; while(ran1%ran2!=0){ ran1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); } chucun7[i]=ran1; chucun8[i]=ran2; for(int m=0;m<i;++m){ if(ran1==chucun7[m]||ran2==chucun8[m]){ zhuangtai=1; break;} zhuangtai=0; } while(zhuangtai==1){ ran1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); ran=ran1/ran2; while(ran1%ran2!=0){ ran1 = (int) (Math.random()*(max-min)+min); ran2=(int) (Math.random()*(max-min)+min); } Chucun7 [I] = RAN1; chucun8 [I] = RAN2; for ( int m = 0; m <I; ++ m) { IF (RAN1 == chucun7 [m] || RAN2 == chucun8 [m]) { zhuangtai =. 1 ; BREAK ; } zhuangtai = 0 ; } // System.out.println(zhuangtai+"??"); } System.out.println(i+","+ran1+" / "+ran2+" ="); break; } } } if(p==hang){ System.out.println("\n"); p=0; } } } }//定制,不能重复
operation result
Function:
Customize the title and the total number of groups
Formula does not repeat
Multiplication result <100
Division are divisible