在 if 之后从某个点重新启动程序

Restarting program from a certain point after an if

我开始学习 Java 不久前,我目前正在尝试制作一个小游戏,看看我是否得到了我所看到的东西。 我想制作一个 "game" 让你在两个具有不同结果的对话选项之间进行选择。 这是我使用的代码:

package programs;

import java.util.Scanner;

public class Programma1_0 {
public static void main(String[] args) {

    System.out.println(
            "You wake up in a laboratory. You don't remember ever being there. You actually don't remember anything.");
    System.out.println("A door opens, a girl comes towards you.");
    System.out.println("Girl:<<Hi, I see you woke up. How are you feeling?>>");
    System.out.println("(Write Good or Bad)");

    Scanner first = new Scanner(System.in);
    String firstch = first.nextLine();

    if (firstch.equals("Good")) {
        System.out.println("Great, we have a lot to explain.");
    } else if (firstch.equals("Bad")) {
        System.out.println("You should be alright in an hour or so. You've slept for a long time.");
    } else {
        System.out.println("(I told you to write Good or Bad)");

    }



    }
}

目前一切正常。唯一的问题是,如果我写的不是好或坏的东西,我会收到消息“(我告诉你写好或坏)”并且程序终止。有没有办法自动重启它?如果我放入更多选择,我希望程序从它终止的问题自动重新启动(所以我没有玩到一半的游戏,错误的问题并且必须从头重新启动程序),是吗可能的? 谢谢

您可以简单地将您的 if-else 语句放在 do-while 循环中,这样您就可以 loop 直到得到正确的响应

int i = 0;
do {
    System.out.println("(Write Good or Bad)");
    firstch = first.nextLine();
    if (firstch.equals("Good")) {
        System.out.println("Great, we have a lot to explain.");
        i = 0;
    } else if (firstch.equals("Bad")) {
        System.out.println("You should be alright in an hour or so. You've slept for a long time.");
        i = 0
    } else {
        System.out.println("(I told you to write Good or Bad)");
        i = 1;
    }
} while (i == 1);

您可以通过将其放在 if 语句之前来完成此操作。

while (true) {
     if (firstch.equals("Good") || firstch.equals("Bad")) 
         break;  
     else {
         System.out.println("(I told you to write Good or Bad)");
         firstch = first.nextLine();
     }
 }

然后您还可以删除 if 语句的最后 else 部分。

现在它将继续请求新的输入,直到它得到 "Good" 或 "Bad"

您可以将程序划分为单独的方法。在这里,我创建了一个名为 retrieveAnswer() 的方法,它的唯一任务是创建 Scanner 并获取输入。此方法将 return 中的 String 视为 public static String header。

我创建的另一个方法名为 getResult(),它接受一个 String 参数,现在将比较从

传递的 String
   String firstch = retrieveAnswer();
   getResult(firstch);

如果结果转到 else 块,它将调用 retrieveAnswer() 并将值 returned 传递给 getResult(),如 getResult(retrieveAnswer()) 中所示然后将重新启动整个过程。

对此有多种解决方案,但我只是采用了递归路线。祝 Java 好运!如果您感到困惑,请多研究方法,因为它们在编程中非常重要。

import java.util.Scanner;

public class Source {

    public static void main(String[] args) {

        System.out.println(
                "You wake up in a laboratory. You don't remember ever being there. You actually don't remember anything.");
        System.out.println("A door opens, a girl comes towards you.");
        System.out.println("Girl:<<Hi, I see you woke up. How are you feeling?>>");
        System.out.println("(Write Good or Bad)");

        String firstch = retrieveAnswer();
        getResult(firstch);

    }

    public static String retrieveAnswer(){
        Scanner first = new Scanner(System.in);
        String firstch = first.nextLine();
        return firstch;
    }
    public static void getResult(String firstch){
        if (firstch.equals("Good")) {
            System.out.println("Great, we have a lot to explain.");
        } else if (firstch.equals("Bad")) {
            System.out.println("You should be alright in an hour or so. You've slept for a long time.");
        } else {
            System.out.println("(I told you to write Good or Bad)");
            getResult(retrieveAnswer());
        }
    }
}