很抱歉提出愚蠢的问题,但我没想到。 我希望重复if else语句,直到获得有效输入然后继续询问下一个问题。 任何人都可以告诉我什么是错的? 代码本身没有任何问题(它可以运行)但它只显示"错误"和#34;请重新输入答案"然后在没有询问" day"的情况下进入下一部分。再次。 提前致谢
public class Test {
public static void main(String[] args) {
String Day; // The user’s name.
TextIO.put("This program is to tell you what to eat for today.");
TextIO.put("\nBefore it starts, can you tell me what's the day today?");
Day = TextIO.getln();{
if (Day.equals("Monday")){
System.out.print("Eat Chocolates");
}
else if (Day.equals("Tuesday")){
System.out.print("Eat Oranges");
}
else if (Day.equals("Wednesday")){
System.out.print("Eat Strawberries");
}
else if (Day.equals("Thursday")){
System.out.print("Eat Lemons");
}
else if (Day.equals("Friday")){
System.out.print("Eat Bananas");
}
else if (Day.equals("Saturday")){
System.out.print("Eat Apples");
}
else if (Day.equals("Sunday")){
System.out.print("Eat Grass");
}
else if (Day.equals(false))
{System.out.print("Error");
System.out.print("\nPlease re-eneter the answer");
Day = TextIO.getln();
}
答案 0 :(得分:2)
最好在多个if:
中使用switch语句boolean error=false;
do{
error=false;
String day="";
switch (day) {
case "Monday":
System.out.print("Eat Chocolates");
break;
case "Tuesday":
System.out.print("Eat Oranges");
break;
.
.
.
default:
System.out.print("Error");
System.out.print("\nPlease re-eneter the answer");
error=true;
break;
}
}
while(error)
答案 1 :(得分:1)
您应该在while
中查看do-while
或Java
。
您可以从以下示例中获得想法..
boolean status = true;
String input = "";
while (status) {
System.out.println("your input\n");
input = new Scanner(System.in).nextLine();
if (input.equals("valid")) {
System.out.println("valid input");
status = false;
} else {
// not a valid input
System.out.println("Please re-try");
}
}
答案 2 :(得分:1)
试试这个:
String day = TextIO.getln();
List<String> daysOfWeek = new ArrayList<String>(Arrays.asList("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"));
while (!daysOfWeek.contains(day)) {
System.out.println("Invalid input.");
day = TextIO.getln();
}
... continue with program after valid input ...
答案 3 :(得分:0)
我从这里找到了一个TextIO课程:http://math.hws.edu/javanotes/source/
然后我编辑了你的代码,看起来像这样......
public class Test {
public static void main(String[] args) {
String Day; // The user’s name.
TextIO.put("This program is to tell you what to eat for today.");
TextIO.put("\nBefore it starts, can you tell me what's the day today?");
Day = TextIO.getln();
if (Day.equals("Monday")){
System.out.print("Eat Chocolates");
}
else if (Day.equals("Tuesday")){
System.out.print("Eat Oranges");
}
else if (Day.equals("Wednesday")){
System.out.print("Eat Strawberries");
}
else if (Day.equals("Thursday")){
System.out.print("Eat Lemons");
}
else if (Day.equals("Friday")){
System.out.print("Eat Bananas");
}
else if (Day.equals("Saturday")){
System.out.print("Eat Apples");
}
else if (Day.equals("Sunday")){
System.out.print("Eat Grass");
}
else if (Day.equals(false))
{System.out.print("Error");
System.out.print("\nPlease re-eneter the answer");
Day = TextIO.getln();
}
}
}
最后我运行了这段代码并得到了这个......
$ java Test
This program is to tell you what to eat for today.
Before it starts, can you tell me what's the day today?Monday
Eat Chocolates
答案 4 :(得分:-1)
使用Java OOD Concepts并为this创建递归方法的最佳方法。并且switch语句比if else梯形图更合适。 你可以通过以下[1]更好地了解java中的递归方法。 只需将您的程序更改为:
public class Test {
public Test() {
}
public void getInput() {
String Day; // The user’s name.
TextIO.put("This program is to tell you what to eat for today.");
TextIO.put("\nBefore it starts, can you tell me what's the day today?");
String day=TextIO.getln();
switch (day) {
case "Monday":
System.out.print("Eat Chocolates");
break;
case "Tuesday":
System.out.print("Eat Oranges");
break;
.
.
.
default:
System.out.print("Error");
System.out.print("\nPlease re-eneter the answer");
getInput();
}
}
public static void main(String[] args) {
Test test = new Test();
test.getInput();
}
}
[1]。 http://danzig.jct.ac.il/java_class/recursion.html
希望这会对你有所帮助。
答案 5 :(得分:-3)
使用循环
public static void main(String[] args) {
String Day; // The user’s name.
TextIO.put("This program is to tell you what to eat for today.");
TextIO.put("\nBefore it starts, can you tell me what's the day today?");
do
{
Day = TextIO.getln();
if (Day.equals("Monday")){
System.out.print("Eat Chocolates");
break;
}
else if (Day.equals("Tuesday")){
System.out.print("Eat Oranges");
break;
}
else if (Day.equals("Wednesday")){
System.out.print("Eat Strawberries");
break;
}
else if (Day.equals("Thursday")){
System.out.print("Eat Lemons");
break;
}
else if (Day.equals("Friday")){
System.out.print("Eat Bananas");
break;
}
else if (Day.equals("Saturday")){
System.out.print("Eat Apples");
break;
}
else if (Day.equals("Sunday")){
System.out.print("Eat Grass");
break;
}
else if (Day.equals(false))
{System.out.print("Error");
System.out.print("\nPlease re-eneter the answer");
}
}while(true);
}