【问题标题】:Trying to use classes in a switch statement尝试在 switch 语句中使用类
【发布时间】:2021-01-29 22:28:00
【问题描述】:

所以我正在尝试编写一个程序来做一些事情,例如,它会为您提供一个可能的命令列表,如果您输入“1”,它应该会将您发送到一个基本的计算器应用程序,但是,我遇到了一个我自己似乎无法解决的泡菜,非常感谢您的帮助

这是我试图从中调用另一个类的类终端:

import java.util.Scanner;

public class Terminal {

    Scanner sc;

    public Terminal() {
        System.out.println("""
                *********************
                TERMINAL APPLICATION
                 Version 0.1 Beta
                      Retr0K
                *********************
                """);
        printCommands();
    }

    public void printCommands() {
        System.out.println("""
                ******************************************
                List of Commands:
                    0.) quit program
                    1.) basic math calculator
                    2.) temperature conversion calculator
                    3.) length conversion calculator
                    4.) quadratic formula calculator
                    5.) random number generator
                    6.) choose your own adventure game
                    7.) rock paper scissors game
                    8.) tic tak toe game
                ******************************************
                """);

        int command = getCommand(10);
        checkCommand(command);
    }

    public int getCommand(int options) {
        sc = new Scanner(System.in);

        System.out.print("Enter a Corresponding Number (0-" + (options-1) + "): ");

        if(sc.hasNextInt()) {
            return sc.nextInt();
        }
        else {
            return -1;
        }
    }

    public void checkCommand(int command) {
        switch (command) {
            case 0 -> System.exit(0);
            case 1 -> new MathCalc();
            case 2 -> new TempCalc();
            case 3 -> new LengthConv();
            case 4 -> new QuadFormulaCalc();
            case 5 -> new RandNumGen();
            case 6 -> new ChooseAdventure();
            case 7 -> new RockPaperScissors();
            case 8 -> new TicTacToe();
            default -> {
                System.out.println("Invalid input, try again");
                printCommands();
            }
        }
    }
    
}

问题在于这段特定的代码:

public void checkCommand(int command) {
        switch (command) {
            case 0 -> System.exit(0);
            case 1 -> new MathCalc();
            case 2 -> new TempCalc();
            case 3 -> new LengthConv();
            case 4 -> new QuadFormulaCalc();
            case 5 -> new RandNumGen();
            case 6 -> new ChooseAdventure();
            case 7 -> new RockPaperScissors();
            case 8 -> new TicTacToe();
            default -> {
                System.out.println("Invalid input, try again");
                printCommands();
            }
        }
    }

特别是当我尝试输入“1”以进入我的 MathCalc() 类时。如果有帮助,这里是 MathCalc() 类的代码:

public class MathCalc extends Terminal {

    public MathCalc() {
        System.out.println("""
                BASIC MATH CALCULATOR APP
                **************************
                """);

        printOperators();
    }

    public void printOperators() {
        System.out.println("""
                Operators:
                    0.) Return to previous screen
                    1.) addition
                    2.) subtraction
                    3.) multiplication
                    4.) division
                    5.) modulus
                """);

        int operator = getCommand(6);
        checkOperator(operator);
    }

    public void checkOperator(int operator) {
        switch (operator) {
            case 0 -> new Terminal();
            case 1 -> add();
            case 2 -> sub();
            case 3 -> mul();
            case 4 -> div();
            case 5 -> mod();
            default -> {
                System.out.println("Invalid Input, try again");
                printOperators();
            }
        }
    }

    public void add() {

    }

    public void sub() {

    }

    public void mul() {

    }

    public void div() {

    }

    public void mod() {

    }

}

这还没有完成,因为我希望在进一步研究之前至少能够访问课程中的内容。

回到问题上来,每当我输入“1”时,而不是将我发送到我的 MathCalc() 类,它似乎创建了一个新的终端类,当我在程序中输入 1 时会发生以下情况: Output after inputting 1 into my program

然而,奇怪的是,当我输入 1 时,前面的东西出现了,然后继续输入 2,我的 TempConv() 类和我的 MathCalc() 类都运行了,看起来是这样的像: Output after inputting 1 and then 2 into my program

真的很困惑为什么会发生这种情况,不胜感激。 谢谢!

【问题讨论】:

    标签: java class switch-statement


    【解决方案1】:

    好吧,原来我没有意识到当你有一个类时会像我一样扩展另一个类:

    public class MathCalc extends Terminal {...}
    

    那么,在这个例子中,当你运行 MathCalc() 时,Terminal() 类的构造函数中的东西也会同时运行。我只想扩展终端,因为我在终端中有一个方法对我的所有其他类都是相同的,但我只是复制并粘贴并停止扩展终端,现在它工作正常。

    【讨论】:

      猜你喜欢
      • 2014-05-20
      • 1970-01-01
      • 2018-10-17
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多