我的程序基本上允许用户输入存储在成绩簿(数组)中的成绩(元素)。用户还可以选择更改成绩簿(数组)中的成绩(元素)。
我的问题是,一旦while循环循环并且用户被要求“进行更多更改?输入是或否”如果我输入yes,则用户询问要更改的等级,是否允许更换等级,并修改成绩簿打印。但是,如果我输入“否”,则仅打印成绩单。有没有办法让程序打印出来“再见!” (如果用户输入no,则表示程序结束)?我相信我应该重新组织我的代码并使用boolean = false设置while循环?但我不确定如何开始...
非常感谢任何帮助。谢谢。
System.out.println("Make changes? Enter Yes or No");
String makeChanges = input.next();
if (makeChanges.equals("no")) {
System.out.println("Good bye!");
}
while (makeChanges.equalsIgnoreCase("Yes")) {
// Ask user if what grade they would like to change
//int index = NumberReader.readPositiveInt(input, "Enter the index of the grade to be changed: (1 to " + grades + ") : ", "Invalid index input", index);
int index = NumberReader.readCappedPositiveInt(input, "Enter the index of the grade to be changed: (1 to " + grades + ") : ", "Invalid index input", numOfGrades);
System.out.println("Enter grade (limit to two decimal places)" + ": ");
// offset the index by one
mogrades[index - 1] = NumberReader.readPositiveDouble(input, "Enter grade " + index + " :",
"Invalid data entered");
System.out.println("The Grade book contains: ");
printArray(mogrades);
System.out.println("Make more changes? Enter Yes or No");
makeChanges = input.next();
System.out.println(makeChanges);
System.out.println("The Grade book contains: ");
printArray(mogrades);
}
}
答案 0 :(得分:0)
Break语句用于退出循环。你可以在while循环中尝试这个:
if (makeChanges.equalsIgnoreCase("No")) {
System.out.println("Good Bye!");
break;
}
如果要退出整个程序,请使用System.exit(0);
if (makeChanges.equalsIgnoreCase("No")) {
System.out.println("Good Bye!");
System.exit(0);
}
这是一个简单的程序,演示了这将如何工作:
import java.util.Scanner;
public class Simple {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String makeChanges = "Yes";
while (makeChanges.equalsIgnoreCase("Yes")) {
System.out.println("Make more changes? Enter Yes or No:");
makeChanges = input.next();
if (makeChanges.equalsIgnoreCase("No")) {
System.out.println("Good Bye!");
System.exit(0);
}
}
}
}
以下是命令行的输出:
daniel@4.3:StackOverflow $ javac Simple.java
daniel@4.3:StackOverflow $ java Simple
进行更多更改?输入是或否:
是
进行更多更改?输入是或否:
是
进行更多更改?输入是或否:
没有
再见!