简体   繁体   English

如何在Java中重新加载主类?

[英]How to reload main class in java?

Actually I am developing a game TicTacToe .In this game when the game ends my form disappears and I need to run it from command line arguments to play it once again.Actually in my logic if I can reload or run the main class then it will be very helpful for me to develop a good game. 实际上,我正在开发一个TicTacToe游戏,在游戏结束时,我的表单消失了,我需要从命令行参数运行它才能再次播放它。实际上按照我的逻辑,如果我可以重新加载或运行主类,它将对我开发一款出色的游戏非常有帮助。 Actually as we know when a java program runs then main class will be called first. 实际上,正如我们所知,当Java程序运行时,将首先调用主类。 * And my question is that What to do if we want to reload or want to run main class again? * 我的问题是,如果我们想重新加载或想再次运行主类怎么办? *Please help me as soon as possible I am waiting form reply. *请等待表格回复,请尽快帮助我。

Let's imagine your main launches a function startGame(), just create a while loop. 假设您的主程序启动了一个函数startGame(),仅创建了一个while循环。

While(true) startGame()

When your game ended, it will start again. 游戏结束后,它将重新开始。

A better way to do this is to add a Menu, that returns true or false if the person wants to play another game. 更好的方法是添加一个Menu,如果此人想玩其他游戏,则返回true或false。

while(showMenu()) {
   startGame();
}

When your game is over you should add buttons to or replace the game window to provide a restart option. 游戏结束后,您应该向游戏窗口添加按钮或替换游戏窗口以提供重新启动选项。 When the user chooses this option you can repaint your game window and start it again. 当用户选择此选项时,您可以重新绘制游戏窗口并重新启动。 The main method only provides a hook to start your application, the application itself needs to handle any retry logic. main方法仅提供一个启动应用程序的挂钩,应用程序本身需要处理任何重试逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM