* 入力スキャンの修正
Enter キー/空白行を使用して while ループを終了しようとしています 以下のテスト コードは正常に動作します。
Scanner scan = new Scanner(System.in);
String line;
while ( (line=scan.nextLine()).length() > 0)
{
System.out.println(line);
}
ただし、メインプログラム内に統合すると (do-while ループ内の int スイッチケース内に配置された while ループ)、プログラムはコード @case2 をスキップし、do-while ループを続行します
int input;
do{
System.out.print("Choose a function (-1 to exit): ");
input=scan.nextInt();
switch (input)
{
case 1:
break;
case 2:
//same while loop here
break;
}
}while(input!=-1);
出力例:
Choose a function (-1 to exit): 1
Choose a function (-1 to exit): 2
//skip case2 coding
Choose a function (-1 to exit): 1
Choose a function (-1 to exit): -1
//program ends