JAVA grammar foundation --- Homework 2

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

 

  Enumeration not belonging to the original type, belonging to a reference type, the value of each of the object-specific reference, the same value reference the same object; using Size.valueOf () to convert from a string of enumerated types, still a reference type; use "== "and" the equals () "is compared; foreach iteration may utilize the output of the enumerated type enum; enumeration type may be used to set the initial data, since the enumeration type can be used for a switch statement, multiple selection branches may be provided.

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

Results achieved: .060000000000000005 0.05 + 0.01 =
     1.0 - 0.42 = 0.5800000000000001
     4.015 * 100 = 401.49999999999994
     123.3 / 100 = 1.2329999999999999

Calculates values ​​using the double type, the value is not accurate, change occurs decimal place. Reason: The question floats stored in the computer. Adding a decimal, the sum is a binary computer, so that the final result to be very close to the value of the results obtained.

public class Practice {

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

}

Results achieved: the Y = X-100200 +
     300 + the Y = X-

The first one is to output the value of X, then the output value Y; second sentence output value Y X +. Because the determination output statement, + X + Y is the output X, Y value, X + Y is an addition operation.

 1 import java.util.Random;
 2 import java.util.Scanner;
 3 
 4 public class count {
 5 
 6     public static void main(String[] args) {
 7         Scanner in = new Scanner(System.in);
 8         int i = 1;
 9         int a;
10         int b;
11         int c = 100;
12         int n = in.nextInt();   //输入题目总数
13         int j = in.nextInt();   //The input line number of the subject 
14          int K;
 15          int m = 0 ;
 16          K = J;
 . 17          the Random R1 = new new the Random ();    // randomly generated numbers 1-99 operation 
18 is          the Random R2 = new new the Random ();    // 1-4 randomly generated number, the control arithmetic. 
. 19          int X [] = new new  int [n-];
 20 is          int Y [] = new new  int [n-];
 21 is          the while {(I <= n-)                // randomly generated. 
22 is              of System.out.print (I + "." );
 23 is             a = r1.nextInt(99)+1;
24             b = r2.nextInt(4);
25             System.out.print(a);
26             if(b==0){
27                 c = r1.nextInt(99)+1;
28                 if(m>0){
29                     for(int t = 0;t<m;t++){
30                         if(a==x[t]&&c==y[t]){
31                             continue;
32                         }
33                     }
34                 }
35                 System.out.print(" + ");
 36                  of System.out.print (C + "=" );
 37 [                  X [m] = A;
 38 is                  Y [m] = C;
 39              }
 40              the else  IF (B ==. 1 ) {
 41 is                  the while (C> A = ) {                  // minuend is greater than if the minuend, regenerated 
42 is                      C = r1.nextInt (99) + 1'd ;
 43 is                  }
 44 is                  IF (m> 0 ) {
 45                      for ( int T = 0; T <m; T ++ ) {
 46 is                          IF (A == X [T] && C == Y [T]) {
 47                             Continue ;
 48                          }
 49                      }
 50                  }
 51 is                  of System.out.print ( "-" );
 52 is                  of System.out.print (C + "=" );
 53 is                  X [m] = A;
 54 is                  Y [m] = C;
 55              }
 56 is              the else  IF (B == 2 ) {
 57 is                  the while (C * A> 100) {                  // If the multiplication is greater than two digits, regenerates 
58                      C = r1.nextInt (99) + 1'd ;
 59                  }if(m>0){
60                     for(int t = 0;t<m;t++){
61                         if(a==x[t]&&c==y[t]){
62                             continue;
63                         }
64                     }
65                 }
66                 System.out.print(" * ");
67                 System.out.print(c+" =  ");
68                 x[m] = a;
69                 y[m] = c;
70             }
71             else if(b==3){
72                  the while (A% C! = 0) {                  // If not divisible, regenerated. 
73 is                      C = r1.nextInt (99) + 1'd ;
 74                  } IF (m> 0 ) {
 75                      for ( int T = 0; T <m; T ++ ) {
 76                          IF (A == X [T] && C == Y [T]) {
 77                              Continue ;
 78                          }
 79                      }
 80                  }
 81                  of System.out.print ( "/" );
 82                  of System.out.print (C + "=" );
 83                 x[m] = a;
84                 y[m] = c;
85             }
86             i++;
87             m++;
88             j--;
89             if(j==0){
90                 System.out.println();
91                 j = k;
92             }
93         }
94         in.close();
95     }
96 
97 }

This code is used to implement the four operations randomly generated. Specific requirements: no larger than the minuend subtrahend; product of not more than two digits; divisible division; expression determined total number of each input line, each equation has a label.

While loop output used to achieve the problem, if statement determines whether the fulfillment of the conditions, using random numbers generated randomly class. Wherein the randomly generated random interval attention class is left and right to open and close the left from zero.

Guess you like

Origin www.cnblogs.com/20183711PYD/p/11567440.html