我正在尝试用Java创建一个简单的文本冒险游戏,当我输入Quit时它会结束游戏。但相反它会询问你是否想要执行任务,即使你输入quit,假设结束代码,我尝试使用循环来修复此错误,但不得不运气。我知道哪里出错了?
import java.util.*;
public class TextGame {
public static void main(String[] args) {
//Boolean run = true;
//while (run){
Scanner in = new Scanner(System. in );
System.out.println("~SPECTRE~");
System.out.println("");
System.out.println("Please Choose One Below");
System.out.println("");
System.out.println("New");
System.out.println("Quit");
System.out.println("");
String option;
System.out.print("Select one of the options here: ");
option = in .next();
if (option.equals("New")) {
System.out.println("Hello adventurer! Welcome to the land of Spectre.");
String name;
System.out.print("What is your name adventurer? ");
name = in .next();
System.out.println("Hello there! " + name);
} else if (option.equals("Quit")) {
System.out.println("*Returns to desktop*");
}
String quest;
System.out.print("Would you like to go on a quest? ");
quest = in .next();
if (quest.equals("Yes")) {
System.out.println("Here is a list of quests that I would like you to do ");
System.out.println("");
System.out.println("1) Fight the evil troll of Port Howlham");
System.out.println("");
System.out.println("2) Deliver supplys to the soliders in need");
System.out.println("");
System.out.println("3) Find the Kings lost son");
System.out.println("");
System.out.println("4) Find the Gemstone of Darlingbee to defeat the evil witch of Hobbitstone");
} else {
option.equals("");
System.out.println("*Please pick New Game or Quit*");
}
}
}
答案 0 :(得分:6)
试试这个:
} else if (option.equalsIgnoreCase("Quit")) {
System.exit(0);
}
或者这个:
} else if (option.equalsIgnoreCase("Quit")) {
return;
}
答案 1 :(得分:2)
您可能希望退出该计划。使用System.exit(0);
else if(option.equals("Quit"))
{
System.exit(0);
}
0
参数是一个状态代码,表示程序正常终止。
答案 2 :(得分:1)
您可以使用System.exit(code)
完成该计划。把它放在else if
块的末尾:
if (option.equals("Quit") {
...
System.exit(0);
}
code
可以是任何整数:
该参数用作状态代码;按照惯例,非零状态代码表示异常终止。