1

コードはコンパイルされますが、メニューにエラーがあるようです。ユーザーが選択肢の 1 つを選択すると、プログラムが実行されますが、選択肢を選択しても何も起こりません。コードは次のとおりです。

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 の外で、入力を 1 回だけ読み取ります (そうしないと、無限ループに陥ってしまいます)。ほとんどの場合、意図は次のwhile ((choice = scan.nextLine()) != "q"); とおりです。実行中に何も表示されない理由は、何が表示されるかによって異なりmyGeek.getName()ます。名前が示すように、単純なゲッターです。この場合、名前は返されますが、画面には何も出力されません。

于 2013-11-01T17:36:45.000 に答える