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

我制作了一个java计算游戏,源码如下(java:1.8):

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 {
            Thread.sleep(2000);
        } 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 {
            Thread.sleep(10000);
        } catch (Exception var1) {
            System.exit(0);
        }
    }
}

但是它的输出却是这样的:

img


求解,实在感谢!

我改了一下可以了,改了几个问题:

  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 {
            Thread.sleep(2000);
        } 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 {
            Thread.sleep(10000);
        } catch (Exception var1) {
            System.exit(0);
        }
    }
}

这段 Java 代码存在以下问题:

在 Java 中,方法名通常使用小写字母,而这里的方法名首字母使用了大写字母,不符合 Java 命名规范。

该程序的主要逻辑存在死循环,无法正常退出。在 while 循环中没有任何退出条件,如果用户输入的难度一直不是 4,程序将一直运行下去。

在每个游戏难度对应的方法中,都使用了一个无限循环,导致题目无法达到指定的数量。例如,在 easy() 方法中,for 循环的条件是 i < 20,但实际上该循环会执行 19 次,而不是 20 次。正确的条件应该是 i <= 20。

在每个游戏难度对应的方法中,程序生成随机数的方式存在问题。例如,在 easy() 方法中,程序使用了 ra.nextInt() 方法来生成随机数,但没有指定上限。因此,程序可能生成一个非常大的随机数,使得用户无法回答问题。应该使用 ra.nextInt(100) 来限制随机数的上限为 100。

在程序的各个方法中,使用了 System.exit(0) 来终止程序。这样做会直接终止程序的运行,而不会进行任何清理工作,容易导致资源泄露等问题。正确的做法是使用 return 语句来结束方法的执行。如果要终止整个程序的运行,可以使用 System.exit(0),但是需要在程序的最后一步使用,以确保所有的资源都被正确释放。

在控制台里运行,否则没法输入数字

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^