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

Java 在if之后从某一点重新启动程序,java,restart,terminate,choice,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

不久前我开始学习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)");

    }



    }
}
打包程序;
导入java.util.Scanner;
公共类程序MA1\U 0{
公共静态void main(字符串[]args){
System.out.println(
“你在实验室里醒来。你不记得曾经去过那里。你实际上什么都不记得。”);
System.out.println(“一扇门打开,一个女孩向你走来。”);
System.out.println(“女孩:”);
System.out.println(“(写好或坏)”);
扫描仪优先=新扫描仪(System.in);
字符串firstch=first.nextLine();
如果(第一条等于(“好”)){
System.out.println(“太好了,我们有很多东西要解释。”);
}否则如果(第一个等于(“坏”)){
System.out.println(“你应该在一个小时左右就好了,你已经睡了很长时间了。”);
}否则{
System.out.println(“(我告诉过你写得好或坏)”;
}
}
}
到目前为止,它正在按预期工作。唯一的问题是,如果我写的东西不是好的或坏的,我会收到消息“(我告诉过你要写好的或坏的)”,程序就会终止。有没有办法自动重启它?如果我输入更多选项,我希望程序从终止的问题自动重新启动(因此我不会玩到游戏的一半,遇到错误的问题,必须从一开始就重新启动程序),这可能吗?
谢谢。

您只需将您的
if else
语句放入
do while
循环中,这样您就可以
循环
直到得到正确的响应

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


现在,它将继续请求新的输入,直到得到“好”或“坏”

您可以将程序划分为不同的方法。在这里,我创建了一个名为
retrieveAnswer()
的方法,它的唯一任务是创建一个
扫描仪
并获取输入。此方法将返回一个
字符串
,如
公共静态字符串
标题所示

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

   String firstch = retrieveAnswer();
   getResult(firstch);
如果结果转到
else
块,它将调用
retrieveAnswer()
并将返回的值传递给
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());
        }
    }
}
import java.util.Scanner;
公共类源{
公共静态void main(字符串[]args){
System.out.println(
“你在实验室里醒来。你不记得曾经去过那里。你实际上什么都不记得。”);
System.out.println(“一扇门打开,一个女孩向你走来。”);
System.out.println(“女孩:”);
System.out.println(“(写好或坏)”);
字符串firstch=retrieveAnswer();
getResult(firstch);
}
publicstaticstringretrieveanswer(){
扫描仪优先=新扫描仪(System.in);
字符串firstch=first.nextLine();
首先返回;
}
公共静态void getResult(字符串firstch){
如果(第一条等于(“好”)){
System.out.println(“太好了,我们有很多东西要解释。”);
}否则如果(第一个等于(“坏”)){
System.out.println(“你应该在一个小时左右就好了,你已经睡了很长时间了。”);
}否则{
System.out.println(“(我告诉过你写得好或坏)”;
getResult(retrieveAnswer());
}
}
}

您的解决方案从不让此人提供新的输入,只会继续写“我告诉过您写得好或坏”是的,我的坏。然后简单地将扫描仪输入移动到循环中。现在已经太晚了。如果文本和代码之间需要一个空行,那么4个空格就可以了。“现在它将继续请求新的输入,直到它得到“好”或“坏”为止。不太可能,因为它无法编译:P@Tom,您是否引用了不正确的语法,例如println?我想我可以解决这个问题,我只是想让大家理解这个方法:我也指的是
stringfirstch=first.nextLine()。哦,是的,刚刚复制了这行,哇:)。我看到你引用了我的其他评论,我只是想帮你,因为这是它发挥作用的一个重要部分。起初,我只是在这里写了半伪代码来表达我的想法,后来把它稍微整理了一下,在这期间我忘记了一些事情:P。。