很抱歉提出愚蠢的问题,但我不知道。
我希望重复if else语句,直到获得有效输入,然后再问下一个问题。
谁能告诉我怎么了?
该代码本身没有任何问题(可以运行),但是仅显示“错误”,并且“请重新输入答案”,然后继续进行下一部分,而无需再次询问“日期”。
提前致谢

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();

        }

最佳答案

使用循环

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);
 }

10-04 17:53