JAVA语法基础---课后作业2

  EnumTest.java:

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

  枚举不属于原始类型,属于引用类型,每一个值引用具体对象,相同值引用同一对象;利用Size.valueOf()从字符串转换成的枚举类型,仍然属于引用类型;可使用“==”和“equals()”进行比较;可利用foreach迭代输出enum中的枚举类型数据;枚举类型可以用于设置初始数据,由于枚举类型可用于switch语句,可提供多选择分支。

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

实现结果:0.05 + 0.01 = 0.060000000000000005
     1.0 - 0.42  = 0.5800000000000001
     4.015 * 100 = 401.49999999999994
     123.3 / 100 = 1.2329999999999999

使用double类型数值进行运算,数值并不精确,小数位出现变动。原因:浮点数在计算机中的存储问题。小数相加时,计算机内是二进制数相加,最后使得结果非常接近应得的结果值。

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

}

实现结果:X+Y=100200
     300=X+Y

第一句是先输出X值,再输出Y值;第二句是输出X+Y的值。因为输出语句判断中,+X+Y是输出X,Y值,X+Y是进行加法运算。

 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();   //输入一行中的题目数
14         int k;
15         int m = 0;
16         k = j;
17         Random r1 = new Random();   //随机生成1-99运算数字
18         Random r2 = new Random();   //随机生成1-4数字,控制加减乘除。
19         int x[] = new int[n];
20         int y[] = new int[n];
21         while(i<=n){               //随机生成。
22             System.out.print(i+".");
23             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                 System.out.print(c+" =  ");
37                 x[m] = a;
38                 y[m] = c;
39             }
40             else if(b==1){
41                 while(c>=a){                 //减数若大于被减数,重新生成
42                     c = r1.nextInt(99)+1;
43                 }
44                 if(m>0){
45                     for(int t = 0;t<m;t++){
46                         if(a==x[t]&&c==y[t]){
47                             continue;
48                         }
49                     }
50                 }
51                 System.out.print(" - ");
52                 System.out.print(c+" =  ");
53                 x[m] = a;
54                 y[m] = c;
55             }
56             else if(b==2){
57                 while(c*a>100){                 //若相乘大于两位数,重新生成
58                     c = r1.nextInt(99)+1;
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                 while(a%c!=0){                 //若无法整除,重新生成。
73                     c = r1.nextInt(99)+1;
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                 System.out.print(" / ");
82                 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 }

此代码用于实现随机生成四则运算。具体要求:减数不能大于被减数;积不大于两位数;除法可以整除;确定运算式的总数,每行输入个数,每个式子都有标号。

使用while循环来达成输出问题,使用if语句判断是否达成条件,利用random类来随机生成数字。其中注意random类的随机生成区间是左闭右开,左从0开始。

猜你喜欢

转载自www.cnblogs.com/20183711PYD/p/11567440.html