不活跃的用户 2023-03-15 22:48 采纳率: 100%
浏览 17
已结题

关于一个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


求解,实在感谢!

  • 写回答

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 {
                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);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥20 mpich安装完成后出问题
  • ¥15 multisim仿真
  • ¥15 stm32循迹小车代码问题
  • ¥15 输入一堆单词,使其去重输出
  • ¥15 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移
  • ¥15 如何用python处理excel的数据(极值标准化)
  • ¥15 三向应力状态求剪应力
  • ¥15 jupyter notebook如何添加libGL.so.1库
  • ¥20 easyPoi能否实现下拉多选或者复选框