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

时间:2015-09-27 20:53:21

标签: java restart terminate choice

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

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

    }



    }
}

到目前为止,它的工作正常。唯一的问题是,如果我写的不是好的或坏的,我得到消息“(我告诉你写好或坏)”并且程序终止。有没有办法自动重启?如果我有更多的选择,我希望程序自动从它终止的问题重新启动(所以我不玩游戏的一半,得到一个错误的问题,并且必须从一开始就重新启动程序),是可能? 感谢。

3 个答案:

答案 0 :(得分:0)

您可以简单地将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);

答案 1 :(得分:0)

你可以把它放在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();
     }
 }

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

现在它将继续要求新的输入,直到它变为“好”或“坏”

答案 2 :(得分:0)

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

我创建的另一个方法是getResult(),它采用String参数,现在将比较从

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

如果结果转到else块,则会调用retrieveAnswer()并将返回的值传递给getResult(),如getResult(retrieveAnswer())中所示,然后重启整个过程

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

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