我有一个总是正确的 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;
}
}