带开关盒的基本Java菜单

时间:2013-11-01 17:22:10

标签: java

代码将编译,但我的菜单似乎有错误。用户将选择其中一个选项并且程序应该执行,但是在选择选择时没有任何反应。这是代码:

import java.util.Scanner;
class Tutorial{
public static void main(String args[]){
Geek myGeek = new Geek("Geek");
System.out.println("Command Options: ");
System.out.println("a: Geek's Name");
System.out.println("b: Num Questions Asked");
System.out.println("c: All Numbers Are the Same");
System.out.println("d: Sum Between Two Integers");
System.out.println("e: Repeat the String");
System.out.println("f: It is Palindrome");
System.out.println("?: Display");
System.out.println("q: Quit");
Scanner scan = new Scanner(System.in);
String choice = scan.nextLine();
do {
switch (choice){
    case "a":
        myGeek.getName();
        break;
    case "b":
        myGeek.getnumberofQuestions();
        break;
    case "c":

        System.out.println("Enter the first number");
        int input1 = scan.nextInt();
        System.out.println("Enter the second number");
        int input2 = scan.nextInt();
        System.out.println("Enter the third number");
        int input3 = scan.nextInt();
        myGeek.allTheSame(input1, input2, input3);
        break;
    case "d":
        System.out.println("Enter the first number");
        int num1 = scan.nextInt();
        System.out.println("Enter the second number");
        int num2 = scan.nextInt();
        myGeek.sum(num1, num2);
        break;
    case "e":
        System.out.println("Enter a string: ");
        String word1 = scan.nextLine();
        System.out.println("Enter an integer: ");
        int numberOfTimes = scan.nextInt();
        System.out.println("Enter the third number");
        myGeek.repeat(word1, numberOfTimes);
        break;
    case "f":
        System.out.println("Enter a string: ");
        String word2 = scan.nextLine();
        myGeek.isPalindrome(word2);
        break;
    case "?":
            System.out.println("Command Options: ");
            System.out.println("a: Geek's Name");
            System.out.println("b: Num Questions Asked");
            System.out.println("c: All Numbers Are the Same");
            System.out.println("d: Sum Between Two Integers");
            System.out.println("e: Repeat the String");
            System.out.println("f: It is Palindrome");
            System.out.println("?: Display");
            System.out.println("q: Quit");
            break;
        }  }while (choice != "q");

}
}

以下是运行时的样子:

http://i.imgur.com/O6SgyH1.png

6 个答案:

答案 0 :(得分:2)

嗯,你肯定需要移动在循环中输入的代码:

        String choice = null;
        Scanner scan = new Scanner(System.in);
        do {
            choice = scan.nextLine();
            switch (choice) {
            case "a":
        .........
            } // end of switch
        } while (!choice.equals("q")); // end of loop

否则,您输入一次并无限期地打开该输入(除非它是“q”)

修改: 您还需要将终止条件更改为while (!choice.equals("q"));
它的工作原理。

答案 1 :(得分:0)

答案 2 :(得分:0)

少数事情:

你只读输入一次 - 在do..while之外 - 可能不是你想要的(否则你会陷入无限循环)。 最有可能的意图是:while ((choice = scan.nextLine()) != "q"); 至于为何在跑步时看不到任何东西,取决于myGeek.getName()的作用。 顾名思义它是一个简单的getter,如果是这种情况,那么它会返回名称,但它不会在屏幕上打印任何内容。

答案 3 :(得分:0)

@rgettman提到的一个问题是,使用String==比较Java中的!=将比较String的对象引用价值;基本上两个String是同一个对象?在这种情况下(大多数情况下),您想要比较该值。

while (choice != "q");更改为while (!choice.equals("q"));以比较值。

略有不同的解释:

现在你正在输入一个字符,说“a”,将你的case与“a”相匹配,然后打开开关/案例。但是,当您的程序到达while时,它基本上会检查choice 是否 "q",以便您的程序返回do/while循环。

答案 4 :(得分:0)

我想你想要这样的东西:

....           
        System.out.println("d: Sum Between Two Integers");
        System.out.println("e: Repeat the String");
        System.out.println("f: It is Palindrome");
        System.out.println("?: Display");
        System.out.println("q: Quit");

        String choice;
        do {
            System.out.println("Select something: ");
            Scanner scan = new Scanner(System.in);
            choice = scan.nextLine();

            switch (choice){
                case "a":
                    myGeek.getName();
                    break;
                case "b":
                    myGeek.getnumberofQuestions();
                    break;
                case "c":

                    System.out.println("Enter the first number");
                    int input1 = scan.nextInt();
                    System.out.println("Enter the second number");
....

如果不是--->>>>你需要做什么 - >而??

并检查,如果你的java是7或更高,并检查你的getMethods() - >如果你回来什么

答案 5 :(得分:-1)

当你输入一些不同的字符串“q”时,你的循环(do {} while(condition))会循环无限,因为条件总是为真。 试试:

while (!choice.equals("q")) {
    switch (choice) {
        case "a":
        myGeek.getName();
        break;
  case "b":
        myGeek.getnumberofQuestions();
        break;
   case "c":

        System.out.println("Enter the first number");
        int input1 = scan.nextInt();
        System.out.println("Enter the second number");
        int input2 = scan.nextInt();
        System.out.println("Enter the third number");
        int input3 = scan.nextInt();
        myGeek.allTheSame(input1, input2, input3);
        break;
    case "d":
        System.out.println("Enter the first number");
        int num1 = scan.nextInt();
        System.out.println("Enter the second number");
        int num2 = scan.nextInt();
        myGeek.sum(num1, num2);
        break;
    case "e":
        System.out.println("Enter a string: ");
        String word1 = scan.nextLine();
        System.out.println("Enter an integer: ");
        int numberOfTimes = scan.nextInt();
        System.out.println("Enter the third number");
        myGeek.repeat(word1, numberOfTimes);
        break;
    case "f":
        System.out.println("Enter a string: ");
        String word2 = scan.nextLine();
        myGeek.isPalindrome(word2);
        break;
    case "?":
        System.out.println("Command Options: ");
        System.out.println("a: Geek's Name");
        System.out.println("b: Num Questions Asked");
        System.out.println("c: All Numbers Are the Same");
        System.out.println("d: Sum Between Two Integers");
        System.out.println("e: Repeat the String");
        System.out.println("f: It is Palindrome");
        System.out.println("?: Display");
        System.out.println("q: Quit");
        break;
    }
}