用JAVA语言编写一个程序,要求如下:
1.可以进行难度选择。一级难度用一位数进行计算,二级难度用二位数进行计算,三级难度用三位数进行计算。
2.可以选择运算类型,包括加、减、乘、除。
3.可以统计成绩。 展开
import java.util.Random;
import java.util.Scanner;
public class T {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int difficulty;//难度
int mode;//运算类型
int answer;//答案
int amount;//挑战题目数量
int score = 0;//得分
System.out.println("请输入难度(1:一位数、2:两位数、3:三位数):");
difficulty = in.nextInt();
System.out.println("请输入运算类型(1:加、2:减、3:乘、4:除):");
mode = in.nextInt();
System.out.println("请输入想要挑战的题目数量:");
amount = in.nextInt();
Random random = new Random();
for (int i = 0; i < amount; i++) {
if (difficulty == 1) {
if (mode == 1) {
int x = random.nextInt(10);
int y = random.nextInt(10);
System.out.println("第" + i + "题:");
System.out.print(x + " + " + y + " = ");
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println("答对了\n");
score++;
} else {
System.out.println("答错了,答案是:" + (x + y) + "\n");
}
} else if (mode == 2) {
int x = random.nextInt(10);
int y = random.nextInt(10);
System.out.println("第" + i + "题:");
System.out.print(x + " - " + y + " = ");
answer = in.nextInt();
if (answer == (x - y)) {
System.out.println("答对了\n");
score++;
} else {
System.out.println("答错了,答案是:" + (x - y) + "\n");
}
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考虑小数的问题
} else {
throw new Exception("运算类型输入值不合法");
}
} else if (difficulty == 2) {
if (mode == 1) {
int x = random.nextInt(100);
int y = random.nextInt(100);
System.out.println("第" + i + "题:");
System.out.print(x + " + " + y + " = ");
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println("答对了\n");
score++;
} else {
System.out.println("答错了,答案是:" + (x + y) + "\n");
}
} else if (mode == 2) {
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考虑小数的问题
} else {
throw new Exception("运算类型输入值不合法");
}
} else if (difficulty == 3) {
if (mode == 1) {
int x = random.nextInt(1000);
int y = random.nextInt(1000);
System.out.println("第" + i + "题:");
System.out.print(x + " + " + y + " = ");
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println("答对了\n");
score++;
} else {
System.out.println("答错了,答案是:" + (x + y) + "\n");
}
} else if (mode == 2) {
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考虑小数的问题
} else {
throw new Exception("运算类型输入值不合法");
}
} else {
throw new Exception("难度输入值不合法");
}
}
System.out.println("挑战结束,您的分数为:" + score);
}
}
我就只举了加法的例子,其他运算的写法都是类似的,你照葫芦画瓢即可
运行结果:
大神可以把完整的代码发出来吗?急着用
包括其他三种计算方法的