课堂测试 1 像二柱子那样, 花二十分钟写一个能自动生成 30 道小学四则运算题目的 "软件"
2 (1) 题目避免重复
(2) 可定制 (数量 / 打印方式)
设计思路
1) 产生两个 1-100 的随机数作为题目参与运算的数据, 产生另一组随机数, 以此随机运算符
2) 运用交换使结果非负.
3) 运用判断, 循环语句保证出题不重复.
4) 运用判断, 循环语句保证结果为正数. 用 for 循环控制出题个数 n, 用循环变量 i 对控制一行中题目的个数.
源代码
- import java.util.Random;
- import java.util.Scanner;
- public class Test {
- static Scanner r = null;
- public static int a = new Random().nextInt(100);
- public static int b = new Random().nextInt(100);
- public static int c = new Random().nextInt(100);
- public static int subjust[][]=new int[100][2] ;
- public static int operator[]=new int[100];
- public static void main(String[] args) {
- r = new Scanner(System.in);
- System.out.println("请输入总题数:");
- int n=r.nextInt();
- System.out.println("请输入每行的题数:");
- int m=r.nextInt();
- for(int i=0;i<n;i++) {
- if((i)%m==0)
- a = new Random().nextInt(100);
- System.out.print(a);
- b = new Random().nextInt(100);
- c = new Random().nextInt(100);
- subjust[i][0]=a;
- subjust[i][1]=c;
- operator[i]=b;
- for(int j=0;j<i;j++) {
- if(subjust[j][0]==a&&subjust[j][1]==c&&operator[j]==b) {
- c = new Random().nextInt(100);
- while(c==subjust[i][1]) {
- c = new Random().nextInt(100);
- }
- c = new Random().nextInt(100);
- while(c==subjust[i][1]) {
- }
- }}
- if(b%4==0)
- System.out.print("+");
- if(b%4==1) {
- System.out.print("-");
- while(a<c) {
- c = new Random().nextInt(100);
- }
- }
- if(b%4==2) {
- System.out.print("*");
- while(a*c>=100) {
- c = new Random().nextInt(100);
- }}
- if(b%4==3) {
- System.out.print("/");
- while(c!=0&&a/c!=0){
- c = new Random().nextInt(100);
- }
- }
- System.out.print("\n");
- System.out.print((i+1)+":");
- System.out.print(c);
- System.out.print("=");
- System.out.print(""+'\t');
- }}}
动手动脑
1. 运行结果
结论
1) 枚举变量中实例化的对象的不同元素的地址不同
2) 可用 "==" 比对枚举变量的值.
2. 结论
java 中的基本数据类型转换分为自动类型转换和强制类型转换. 强制类型转换可能会有存储精度的损失. 自动类型转换不会有存储精度的损失.
3. 结果
结论
意外, 原因: 变量的存储方式不同, 不同浮点数在计算机中存的尾数不同
4. 结果
结论
System.out.println 里面的加号可用于连接字符串 , 假如需要连接的字符串比较长此时也可用加号进行换行连接.
本题中第一个遇到的是字符串, 所以后面的 X,Y 也转换为字符转连接输出了.
第二个则是第一个遇到的 X 与第二个遇到的 Y 是 int 型第三个是字符串所以连接输出了运算结果和字符串.
来源: http://www.bubuko.com/infodetail-3205424.html