1

代码将编译,但我的菜单似乎有错误。The user will select one of the choices and the program should execute, but When choosing a selection nothing happens. 这是代码:

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

4

6 回答 6

3

好吧,您肯定需要移动在循环内输入的代码:

        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"));
为它才能工作。

于 2013-11-01T17:50:21.143 回答
0

一些事情:

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

于 2013-11-01T17:36:45.000 回答
0

这也取决于他们使用的 JDK 版本。

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

http://java.dzone.com/articles/new-java-7-feature-string

于 2013-11-01T17:31:17.467 回答
0

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

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

稍微不同的解释:

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

于 2013-11-01T17:54:09.667 回答
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");
....

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

并检查您的 java 是否为 7 或更高,并检查您的 getMethods() -> 是否返回任何内容

于 2013-11-01T17:55:39.237 回答
-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;
    }
}
于 2013-11-01T17:45:52.543 回答