0

我正在尝试制作一个简单的基于文本的操作系统,但我无法弄清楚为什么我的代码在计算器类完成后不允许我输入命令。它应该继续执行代码,直到我输入“off”,但事实并非如此。Eclipse 说它正在运行,但我无能为力。有人可以帮帮我吗?

这是我的两门课:

public class Calculator extends Start{

public static void calStrt() {

    System.out.print("\nEnter operator you wish to use: ");
    StringInput = scan.nextLine();

    if (StringInput.equals("+")) {

        add();

    } else if (StringInput.equals("-")) {

        sub();

    } else if (StringInput.equals("*")) {

        mul();

    } else if (StringInput.equals("/")) {

        div();

    } else {

        System.out.println("\nSyntax error: Operator not recognized");
        System.out.println("Please try again");
        calStrt();

    }

}

public static void add() {

    System.out.print("\nEnter first number: ");
    intInput = scan.nextInt();
    int intVar1 = intInput;

    System.out.print("\nEnter second number: ");
    intInput = scan.nextInt();
    int intVar2 = intInput;

    System.out.println("\nAnswer: " + (intVar1 + intVar2));

}

public static void sub() {

    System.out.print("\nEnter first number: ");
    intInput = scan.nextInt();
    int intVar1 = intInput;

    System.out.print("\nEnter second number: ");
    intInput = scan.nextInt();
    int intVar2 = intInput;

    System.out.println("\nAnswer: " + (intVar1 - intVar2));

}

public static void mul() {

    System.out.print("\nEnter first number: ");
    intInput = scan.nextInt();
    int intVar1 = intInput;

    System.out.print("\nEnter second number: ");
    intInput = scan.nextInt();
    int intVar2 = intInput;

    System.out.println("\nAnswer: " + (intVar1 * intVar2));

}

public static void div() {

    System.out.print("\nEnter first number: ");
    intInput = scan.nextInt();
    int intVar1 = intInput;

    System.out.print("\nEnter second number: ");
    intInput = scan.nextInt();
    int intVar2 = intInput;

    System.out.println("\nAnswer: " + (intVar1 / intVar2));

}

}




import java.util.Scanner;

class Start {

static Scanner scan = new Scanner(System.in);
static String StringInput;
static int intInput;

public static void main(String[] args) {

    System.out.println("\nWelcome to RobOS");

    passLoop: while (true) {

        System.out.print("\nPlease enter password: ");
        StringInput = scan.nextLine();

        if (StringInput.equals("banana")) {

            System.out.print("Logging in, please wait");
            System.out.print(".");
            System.out.print(".");
            System.out.println(".");

            System.out.println("\nWelcome User");

            outerLoop: while (true) {

                System.out.println("\nType \"help\" to see a list of programs");

                StringInput = scan.nextLine();  

                innerLoop: while (true) {

                    if (StringInput.equalsIgnoreCase("cal")) {

                        Calculator.calStrt();
                        continue outerLoop;

                    } else if (StringInput.equalsIgnoreCase("guess")) {

                        GuessGame.guess();
                        continue outerLoop;

                    } else if (StringInput.equalsIgnoreCase("help")) {

                        System.out.println("\n\"cal\" uses the calculator");
                        System.out.println("\"guess\" plays guessing game");
                        System.out.println("\"help\" shows list of programs");
                        System.out.println("\"off\" turns RobOS off");
                        continue outerLoop;

                    } else if (StringInput.equalsIgnoreCase("off")){

                        break passLoop;

                    } 

                }

            }

        } else { 

            System.out.println("\nWrong password. Please try again");
            continue passLoop;

        }

    }

}

}
4

2 回答 2

2

布伦特纳什是正确的。但是,要修复错误,请尝试使用而不是scan.nextInt()Integer.parseInt(scan.nextLine()); 希望这有效

于 2012-12-11T07:40:39.957 回答
1

您的代码进入无限循环。当你打电话StringInput = scan.nextLine()时,第一次它工作正常。我输入了cal,我可以运行一次计算器。问题是第二次scan.nextLine()被调用时,它会自动返回一个空字符串 "" 作为StringInput. 您的if/else语句集while(true)无法处理此问题,因此它只会永远循环。

更深层的原因是您调用scan.nextInt()读取数字,但问题是当您读取第二个数字以进行计算器操作时,仍然有一个“\n” System.in。结果,当您循环并scan.nextLine()再次调用时,它不会提示您进行任何操作,因为它只是读取仍然坐在上面的“\n”,System.in然后将您送入无限循环。

于 2012-04-21T20:20:26.343 回答