2020年10月3日Java学习日记

四则运算

 

(4)定制是否有乘除法

 

package day02;
 
import java.util.Random;
import java.util.Scanner;
 
public class Test {
 
    public static void main(String args[]) {
 
        Random rand = new Random();
        boolean[] bool = new boolean[101];
        String[] fuhao = new String[] { "+", "-", "*", "/" };
 
        int n = 0;
        int length = 0;
        @SuppressWarnings("resource")
        Scanner scan = new Scanner(System.in);
 
        System.out.print("您想练习题目的个数为:");
        length = scan.nextInt();
 
        System.out.print("操作数的个数为:");
        n = scan.nextInt();
 
        System.out.println("题目中是否出现乘除号:1/0");
        int chengchu = scan.nextInt();
     
        if (chengchu == 1) {
 
            int[] Number = new int[n];
            String[] Fh = new String[n];
 
            for (int i = 0; i < length; i++) {
                System.out.print(i + 1 + "、");
                for (int j = 0; j < n; j++) {
 
                    do {
                        Number[j] = 1+rand.nextInt(100);
                        Fh[j] = String.valueOf(fuhao[rand.nextInt(4)]);
 
                    } while (bool[Number[j]]);
 
                    bool[Number[j]] = true;
 
                    if (j != n - 1) {
                        System.out.print(Number[j] + Fh[j]);
                    } else {
                        System.out.print(Number[j]);
                    }
                }
                System.out.println();
 
            }
 
        }
 
        if (chengchu == 0) {
 
            int[] Number = new int[n];
            String[] Fh = new String[n];
 
            for (int i = 0; i < length; i++) {
                System.out.print(i + 1 + "、");
                for (int j = 0; j < n; j++) {
 
                    do {
                        Number[j] = rand.nextInt(101);
                        Fh[j] = String.valueOf(fuhao[rand.nextInt(2)]);
 
                    } while (bool[Number[j]]);
 
                    bool[Number[j]] = true;
 
                    if (j != n - 1) {
                        System.out.print(Number[j] + Fh[j]);
                    } else {
                        System.out.print(Number[j]);
                    }
                }
                System.out.println();
 
            }
 
        }
    }
}

 

(5) 定制是否有括号(随机插入)

 不会

(6) 定制数值范围

 

System.out.print("数字的最小值为:");
        int min=scan.nextInt();
         
System.out.print("数字的最大值为:");
        int max=scan.nextInt();
         
.........
 
 
Number[j] = rand.nextInt(max+1-min)+min;

 

posted @ 2020-10-03 01:15  Zwyooo  阅读(73)  评论(0编辑  收藏  举报