// 获悔培顷取min - max之间 n个随机数碧陆,然中余后排序
public static int[] orderedRandom(int min, int max, int n) {
if (n >(max - min + 1) || max <min) {
return null
}
int[] result = new int[n]
int count = 0
while (count <n) {
int num = (int) (Math.random() * (max - min)) + min
boolean flag = true
for (int j = 0j <nj++) {
if (num == result[j]) {
flag = false
break
}
}
if (flag) {
result[count] = num
count++
}
}
return Arrays.stream(result).sorted().toArray()
}
public class test {public static void main(String[] args) {
// java实现随机出题
// 50以内的加减乘除郑此法,用户解答后系统判断烂陆对错,用户输入-1时中止训练
Scanner scan = new Scanner(System.in)
char[] arr = {'+','-','*','÷'}
while (true) {
// 生成一个随机小数,范围从 2.25 到 2.75的两位小喊历迅数
// double sum = Math.random()*(2.75-2.25)+2.25
int a = (int) (Math.random()*(50-0)+0)
int b = (int) (Math.random()*(50-0)+0)
int f = (int) (Math.random()*(4-0)+0)
if (f== 1 &&a <b) {
continue
}
if (f== 3 &&a == 0) {
continue
}
System.out.println("题目:"+a+arr[f]+b+"=\n请输入:")
double sum = 0
if (f == 0) {
sum = a+b
}
if (f == 1) {
sum = a-b
}
if (f == 2) {
sum = a*b
}
if (f == 3) {
sum = 1.0*a/b
}
double s = scan.nextDouble()
if (s == -1) {
System.err.println("结束")
break
}
if (s == sum) {
System.err.println("正确")
}else {
System.err.println("错误")
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)