import java.util.*; import java.util.Random; public class SiZe { /** * @param args */ public static void main(String[] args) { String[] str_1 = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; String[] str_2 = new String[] { "+", "-", "*", "/"}; int index = 0; int index_1 = 0; int index_2 = 0; int[] a = new int[2]; double data = 0; double answer = 0; System.out.println("以下有20道简单的四则运算!"); for(int k = 0; k < 20; k++) { System.out.print("第"+(k+1)+"道题:"); for(int i = 0; i < 2; i++) { int j = 0; index_1 = new Random().nextInt(9); if(str_2[index_2].equals("/")) { while(i >= 2 && index_1 == 0) { index_1 = new Random().nextInt(9); } } a[i] = index_1; System.out.print(str_1[index_1]); if(i != 1) { index_2 = new Random().nextInt(4); System.out.print(str_2[index_2]); } } System.out.println(); System.out.print("请输入答案:"); Scanner input = new Scanner(System.in); data =input.nextDouble(); switch(index_2) { case 0: answer = a[0] + a[1]; break; case 1: answer = a[0] - a[1]; break; case 2: answer = a[0] * a[1]; break; case 3: answer = (double)a[0] / a[1]; break; } if(data == answer) System.out.println("你输入的答案正确!"); else System.out.println("你输入的答案错误!"); System.out.println(); } } }
时间: 2024-10-25 17:33:00