java需要帮助才能在不终止程序的情况下重新运行我的主类
所以基本上,我试图在一个类中使用IF语句制作一个小游戏,到目前为止,一切都很顺利。我最大的问题是“后退”提示。我试过这个系统。退出(0),但因为它会终止程序,所以这不是答案。我的问题是,你如何“重新管理”这个班级?例如,当您在“选项”菜单中时,如何返回主菜单
import java.util.Scanner;
public class Main {
public static void main (String[]args)
{
//Main Menu Prompt.
System.out.println ("Welcome to my mini parkour game!");
System.out.println ("Decide which trick to use, and don't mess up!");
System.out.println ("Choose one: Play, Quit");
Scanner bruh = new Scanner (System.in);
String MainMenu = bruh.nextLine ();
// Quit Prompt.
if (MainMenu.equals ("Quit"))
{
System.out.println ("You have quit the game.");
System.exit (0);
}
// Play Prompt.
if (MainMenu.equals ("Play"))
{
System.out.println ("Proceed to level 1, 2, 3, 4, 5, 6, 7, 8, 9, 10");
Scanner playgame = new Scanner (System.in);
String LevelSelect = playgame.nextLine ();
if (LevelSelect.equals ("1"))
{ System.out.println("You have reached this block of code!");
}
}
//code block to remind that java is case sensitive.
else {
System.out.println("error, try picking one again. (case sensitive.)");
return new Main();
}
}
}
我希望输出在主菜单提示符下,即使是在播放提示符下,也不会终止程序
# 1 楼答案
我相信,一个由用户输入控制的while循环(存储在变量中)会起到神奇的作用。你必须学会如何使用while循环
如果你想升级到一个神级Java程序员,可以使用enum定义你的状态,然后使用switch case在内部创建while循环状态机。 我太懒了,无法在手机上输入有效代码,但在较高级别上是这样的:
这是一个学习和阅读java规范的过程,但是,嘿,在这样做之后,你将能够为……编写一个有效的算法。。。。电梯! 祝你好运