# 关于一个Java计算游戏的问题

``````import java.util.Scanner;
import java.util.Random;

public class GameMain {
public static void main(String [] args){
Scanner a = new Scanner(System.in);
System.out.print("欢迎来到数学小游戏");//输出
Wait();
cls();
while(true) {
System.out.print("请输入难度(1=简单,2=中等,3=困难,4=退出) />");
int b = a.nextInt();
if (b == 1) {
System.out.print("正在为您切换为 简单");
easy();
} else if (b == 2) {
System.out.print("正在为您切换为 中等");
zhongdeng();
} else if (b == 3) {
System.out.print("正在为您切换为 困难");
hard();
} else if (b == 4) {
System.out.print("正在退出中...");
Wait();
Exit();
}
}
}
public static void easy(){
int score = 0;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
int a,b,c,d;
cls();
System.out.print("简单共有20题,为50以下的加法");
for(int i = 1;i < 20;i++){
a = ra.nextInt(50);
b = ra.nextInt(50);
System.out.print(a + "+" + b + "=");
c = (a + b);
d = ra.nextInt();
ln();
if(d == c){
score = (score + 10);
System.out.print("正确!加10分!");
}else{
System.out.print("哎,别灰心,答案是" + c);
}
}
cls();
ln();
System.out.println("一共有200分,你获得了" + score + "分");
System.out.print("10秒后自动退出...");
Wait10s();
Exit();
}
public static void zhongdeng(){
System.out.print("中等共有50题,为500下的加法");
int score = 0;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
int a,b,c,d;
cls();
for(int i = 1;i < 50;i++){
a = ra.nextInt(500);
b = ra.nextInt(500);
System.out.print(a + "+" + b + "=");
c = (a + b);
d = ra.nextInt();
ln();
if(d == c){
score = (score + 10);
System.out.print("正确!加10分!");
}else{
System.out.print("哎,别灰心,答案是" + c);
}
}
System.out.println("一共有500分,你获得了" + score + "分");
System.out.print("10秒后自动退出...");
Wait10s();
Exit();
}
public static void hard(){
System.out.print("困难共有100题,为5000以下的加法");
int score = 0;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
int a,b,c,d;
cls();
for(int i = 1;i < 100;i++){
a = ra.nextInt(5000);
b = ra.nextInt(5000);
System.out.print(a + "+" + b + "=");
c = (a + b);
d = ra.nextInt();
ln();
if(d == c){
score = (score + 10);
System.out.print("正确!加10分!");
}else{
System.out.print("哎,别灰心,答案是" + c);
}
}
System.out.println("一共有1000分,你获得了" + score + "分");
System.out.print("10秒后自动退出...");
Wait10s();
Exit();
}
private static void Wait(){//等待
try {
} catch (Exception var1) {
System.exit(0);
}
}
private static void ln(){
System.out.println();//换行
}
public static void cls(){
for(int i = 1;i < 50;i++){
System.out.println();
}
}
private static void Exit(){
System.exit(0);
}
private static void Wait10s(){//等待
try {
} catch (Exception var1) {
System.exit(0);
}
}
}

``````

• 写回答

#### 4条回答默认 最新

• CL有毒 2023-03-16 09:08
关注

我改了一下可以了，改了几个问题：

1. `easy()` `zhongdeng()` `hard()`三个方法内的`Scanner sc = new Scanner(System.in);`声明了但是没有使用，答案`d`是通过`d = ra.nextInt();`自动生成的，不是等待用户输入的，所以选择模式后用户还没输入系统就判定结果了，并且都是0分；
2. `easy()` `zhongdeng()` `hard()`三个方法内的for循环次数都少了一次，`for(int i = 1;i < 20;i++)`只会循环19次，因此全对也只有190分，需要改成`for(int i = 1;i <= 20;i++)`才正确
3. 将部分`System.out.print`改成了`System.out.println`，输出更清晰好看
``````public class GameMain {
public static void main(String [] args){
Scanner a = new Scanner(System.in);
System.out.print("欢迎来到数学小游戏");//输出
Wait();
cls();
while(true) {
System.out.print("请输入难度(1=简单,2=中等,3=困难,4=退出) />");
int b = a.nextInt();
if (b == 1) {
System.out.print("正在为您切换为 简单");
easy();
} else if (b == 2) {
System.out.print("正在为您切换为 中等");
zhongdeng();
} else if (b == 3) {
System.out.print("正在为您切换为 困难");
hard();
} else if (b == 4) {
System.out.print("正在退出中...");
Wait();
Exit();
}
}
}
public static void easy(){
int score = 0;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
int a,b,c,d;
cls();
System.out.println("简单共有20题,为50以下的加法");
for(int i = 1;i <= 20;i++){
a = ra.nextInt(50);
b = ra.nextInt(50);
System.out.print(a + "+" + b + "=");
c = (a + b);
d = sc.nextInt();
ln();
if(d == c){
score = (score + 10);
System.out.println("正确!加10分!");
}else{
System.out.println("哎,别灰心,答案是" + c);
}
}
cls();
ln();
System.out.println("一共有200分,你获得了" + score + "分");
System.out.print("10秒后自动退出...");
Wait10s();
Exit();
}
public static void zhongdeng(){
System.out.println("中等共有50题,为500下的加法");
int score = 0;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
int a,b,c,d;
cls();
for(int i = 1;i <= 50;i++){
a = ra.nextInt(500);
b = ra.nextInt(500);
System.out.print(a + "+" + b + "=");
c = (a + b);
d = sc.nextInt();
ln();
if(d == c){
score = (score + 10);
System.out.println("正确!加10分!");
}else{
System.out.println("哎,别灰心,答案是" + c);
}
}
System.out.println("一共有500分,你获得了" + score + "分");
System.out.print("10秒后自动退出...");
Wait10s();
Exit();
}
public static void hard(){
System.out.println("困难共有100题,为5000以下的加法");
int score = 0;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
int a,b,c,d;
cls();
for(int i = 1;i <= 100;i++){
a = ra.nextInt(5000);
b = ra.nextInt(5000);
System.out.print(a + "+" + b + "=");
c = (a + b);
d = sc.nextInt();
ln();
if(d == c){
score = (score + 10);
System.out.println("正确!加10分!");
}else{
System.out.println("哎,别灰心,答案是" + c);
}
}
System.out.println("一共有1000分,你获得了" + score + "分");
System.out.println("10秒后自动退出...");
Wait10s();
Exit();
}
private static void Wait(){//等待
try {
} catch (Exception var1) {
System.exit(0);
}
}
private static void ln(){
System.out.println();//换行
}
public static void cls(){
for(int i = 1;i < 50;i++){
System.out.println();
}
}
private static void Exit(){
System.exit(0);
}
private static void Wait10s(){//等待
try {
} catch (Exception var1) {
System.exit(0);
}
}
}
``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

• 系统已结题 3月24日
• 已采纳回答 3月16日
• 创建了问题 3月15日

#### 悬赏问题

• ¥15 给出完整的python代码并解释
• ¥15 有人懂怎么做大模型的客服系统吗？卡住了卡住了
• ¥20 firefly-rk3399上启动卡住了
• ¥15 如何删除这个虚拟音频
• ¥50 hyper默认的default switch