0

ユーザーに文字の入力を求める、常にtrueのwhileループがあります。文字が入力されると、switchステートメントで検索され、さらに入力が要求されるswitchステートメントからコードのブロックが実行されます。コードの出力は、コードの一部でユーザーの入力として登録されているようです。これが私のコードの簡単なサンプルです:

while (true) {
    System.out.print("Enter a letter :: ");
    selection = keyboard.nextLine();
    selection = selection.toUpperCase();

    switch (selection) {
    case "A":
        A = null;
        A = new Matrix();
        System.out.println("Default matrix A initialized.");
        break;

    case "C":
        System.out.print("Enter the number of rows (rows > 0) :: ");
        rows = keyboard.nextInt();
        System.out
                .print("Enter the number of columns (colums > 0) :: ");
        cols = keyboard.nextInt();
        System.out
                .print("Enter the initial value for each element :: ");
        initialValue = keyboard.nextInt();

        C = null;
        C = new Matrix(rows, cols, initialValue);
        System.out.println("Matrix C initialized.");
        break;
    }
}
4

1 に答える 1

0

nextInt()次のintを取得しますが、改行文字を読み取りません。ケース「C」で使用しているソースでは、次の行に移動するためnextInt()に別のソースを含める必要があります。nextLine()

case "C":
 ...
 ...
 keyboard.nextLine();
break;
于 2013-02-25T04:19:12.337 に答える