课堂测试及动手动脑

一、课堂测试

1,题目:课堂测试:像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 “软件”

      要求:(1)题目避免重复;

                   (2)可定制(数量/打印方式)

2、思路

        1、使用三个随机数来确定不同的数字,其中两个是为了除数和被除数不同,另一个随机数是为了确定不同的算术符号。

  2、通过for循环来输出n个不同的题。

  3、使用while循环来确定他是不重复的题目每次出来的随机数记录在数组中在第一个for循环中再加入for循环已确定题目数不会重复的。如果重复他讲重新生成随机数直到满足条件。

  4、通过switch来确定不同的算术符号。

3、代码

package test;
import java.math.*;
import java.util.Scanner;
public class Math {
public static void main(String[] args) {
    int sum,i,j,f,m,n;
    System.out.println("请输入你要的题数目");
    Scanner c=new Scanner(System.in);
    int a[]=new int [1000000];
    int b[]=new int [1000000];
    int x;
    sum=c.nextInt();
    System.out.println("请输入几个换行");
    x=c.nextInt();
    int p[]=new int [10000000];
    for(i=0;i<sum;i++)
    
    {
        
        f=(int)(java.lang.Math.random()* 99)+1;
        a[i]=f;
        m=(int)(java.lang.Math.random()*99)+1;
        b[i]=m;
        while(f>m)
        {    f=(int)(java.lang.Math.random()* 99)+1;
        a[i]=f;
        m=(int)(java.lang.Math.random()*99)+1;
        b[i]=m;
        }
        while(f*m>=100)
        {
            f=(int)(java.lang.Math.random()* 99)+1;
            a[i]=f;
            m=(int)(java.lang.Math.random()*99)+1;
            b[i]=m;
            
        }
        while(f/m!=0)
        {
            f=(int)(java.lang.Math.random()* 99)+1;
            a[i]=f;
            m=(int)(java.lang.Math.random()*99)+1;
            b[i]=m;
        }
        n=(int)(java.lang.Math.random()*4);
        p[i]=n;
        for(j=0;j<i;j++)
        {
        if(f==a[i]&&m==b[i]&&n==p[i])
        {f=(int)(java.lang.Math.random()* 99)+1;
        a[i]=f;
        m=(int)(java.lang.Math.random()*99)+1;
        b[i]=m;
        
            
        }
        }
        System.out.print("第"+i+"题");
        n=(int)(java.lang.Math.random()*4);
        
        switch(n)
        {case 0:
                System.out.print(f+"*"+m);break;
        case 1:
            System.out.print(f+"+"+m);        break;
        case 2:System.out.print(f+"/"+m);break;
        case 3:System.out.print(f+"-"+m);break;
        
        
        
        }
        System.out.print("    ");
        if(((i+1)%x)==0)
        {
            System.out.println();
        }
        
        }
    
}


}

4、运行测试

 二、动手动脑1

仔细阅读示例:EnumTest.java,运行它,分析运行结果?

package class1;

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

运行测试

结论

1枚举类型可以只用=号赋值

2从字符串中转换的枚举变量中实例化的对象的的元素赋给新的变量和原变量的地址相同。

三、动手动脑2

public class Test {
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如果前边是String类型的通过相加后边也自动转换为String类型。

2第二个因为是想相加所以不会转换为String类型。

四、动手又动脑3

同名屏蔽

package class1;

public class test1 {
    int a=100;
public static void main(String[] args) {
int a =1000000;
System.out.println(a);

}
}

运行测试

 总结:

  线作用域小的再作用域大的。

五、动手实验

精度丢失

package class1;



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

运行测试

结果令人意外。

1.浮点型的数据为什么会精度丢失, 或者说精度不精确呢? 其实是由于我们代码在程序里写的十进制小数,而计算机内部只能用二进制的小数, 所以无法精确的表达。

2.对于二进制小数,小数点右边能表达的值是 1/2, 1/4, 1/8, 1/16, 1/32, 1/64, 1/128 … 1/(2^n)。所有这些小数都是一点一点的拼凑出来的一个近似的数值, 所有才会不准确的。

原文地址:https://www.cnblogs.com/guziteng1/p/11540367.html