一行破坏了我的整个代码,我不确定为什么,似乎无故调用了一个旧函数

问题描述

因此,如果我们在代码中关注这些功能的进展,就会遇到问题。

questionTime()
menuValid()
keepCounting()

然后在keepCounting()函数中,行int q1_input = q1guess.nextInt();似乎将所有循环循环回questionTime(),因为“请选择一个有效的选项已被打印”,我假设它是扫描仪问题,但无法工作

这里有我的代码可供参考:

import java.util.Scanner;
import java.util.Random;
public class Menu {

    public static void questionTime() {
        Scanner scan = new Scanner(system.in);
        try {int menu_input = scan.nextInt();
            if(menu_input>4 && menu_input < 9){
                scan.close();
                System.out.println("Please pick a valid option.");
                questionTime();
            }
            if(menu_input>9 || menu_input<1){
                scan.close();
                System.out.println("Please pick a valid option.");
                questionTime();
            }
            else{
                scan.close();
                menuValid(menu_input);
            }

        } catch(Exception e) {
            scan.close();
            System.out.println("ths one");
            questionTime();

            }
    }

    public static void menuValid(int menu_input) {
        System.out.println(menu_input);
        if(menu_input == 1){
            keepCounting();
        }

    }

    public static void main(String[] args) {

        System.out.println("P4CS Mini Applications");
        System.out.println("----------------------");
        System.out.println("Please Select an option:");
        System.out.println("1) Keep Counting Game");
        System.out.println("2) Number Conversion Tool");
        System.out.println("3) Universal Product Code (UPC) Calculator");
        System.out.println("4) Universal Product Code (UPC) Checker");
        System.out.println("9) Quit");
        System.out.println("Please enter option");

        questionTime();

        }

    public static void keepCounting(){
        System.out.println("Keep Counting");
        System.out.println("-------------");
        System.out.println("You will be presented with 8 addition questions. After the first question,the left-hand operand is the result of the prevIoUs addition");
        System.out.println("Press enter to start...");

        Scanner scan = new Scanner(system.in);
        String ETS;
        boolean pass = false;
        while(pass == false){
            ETS = scan.nextLine();
            if(ETS.equals("")){
            pass = true;
            }
        scan.close();

        }

        Random rand = new Random();
        int max = 11;
        int maxPOM = 2;

        int LHN = 0;
        int RHN = 0;

        while(LHN == 0 || RHN == 0){
            LHN = rand.nextInt(max);
            RHN = rand.nextInt(max);
        }

        int POM = rand.nextInt(maxPOM);

        int ans1 = 0;
        
        if(POM == 0){
            System.out.println("Question 1: " + LHN + " + " + RHN + " =");
            ans1 = (LHN + RHN);
        }
        if(POM == 1){
            System.out.println("Question 1: " + LHN + " - " + RHN + " =");
            ans1 = (LHN - RHN);
        }

        System.out.println(ans1);

        Scanner q1guess = new Scanner(system.in);
        int q1_input = q1guess.nextInt();
        q1guess.close();
 
    }

}

解决方法

当您关闭连接到Scanner的{​​{1}}时,您还将同时关闭System.in本身:任何后续的读取输入数据的尝试都会静默地不产生任何数据,因为流已关闭

我没有对您的程序做很多事情,而是在您的main方法中为System.in创建了Scanner并将其传递给其他方法。我还删除了对System.in的显式调用,并在scan.close()实现Scanner时实现了try-with-resources。

AutoCloseable