如何从方法重新启动程序

问题描述 投票:0回答:4

我是Java初学者,我正在制作一个基本游戏来练习。我快完成了,但我还有一个障碍需要克服。

我想知道如何在询问是否结束游戏时按

no
作为选择后,在game()方法上循环游戏。

这是我的代码:

private static void game() //game method
{
    //...

   int play = JOptionPane.showOptionDialog(null
                   ,"End"
                   , "Do you want to play again?"
                   , JOptionPane.PLAIN_MESSAGE
                   ,JOptionPane.DEFAULT_OPTION
                   , null
                   , again
                   , again[1]);
   //end of game

    if (play == 0)
        System.exit(0);//exit
    else
         /* what do I put here to restart the program in the same method(game()) 
            after pressing the No button on the JOptionPane???    */        
         System.out.println("Service not available");  

对于任何可以提供帮助的人,我非常感谢!

java loops methods restart
4个回答
3
投票

鉴于程序的当前状态,最简单最简单直接可读的方法是递归。只需再次调用您的游戏方法即可。请注意,可能存在递归限制,因此循环是推荐的方法,即使它确实涉及稍微重构代码。

else{
    game();
}

循环方法:在开头声明

play
并使用循环:

private static void game(){
    boolean play = true;
    while (play){
        //...
        //find out if user wants to play again
        //set play to false if player doesn't want to play anymore
    }
}

2
投票

JOptionPane
函数代码中提取
game()
部分:

int play = 0;

do {
  game();
  play = JOptionPane.showOptionDialog(
    null,
    "End",
    "Do you want to play again?",
    JOptionPane.PLAIN_MESSAGE,
    JOptionPane.DEFAULT_OPTION,
    null,
    again,
    again[1]
  ); 
} while (play);

1
投票

如果你只是想让它发挥作用,你可以这样做:

private static void game()//game method
{
    boolean exit = false;
    while(!exit){
        //...int play = JOptionPane.showOptionDialog(null,"Play Again?", "Do you want to play again?", JOptionPane.PLAIN_MESSAGE,JOptionPane.DEFAULT_OPTION, null, again, again[1]);
       //end of game


        if (play == 0) {
            exit = true;
        }

    }          
        System.exit(0);//exit 

但是更好更专业的方法是重构你的代码,这样你就可以提取游戏逻辑并将其与用户对话框交互分开。


0
投票

您可以使用这种方法,我非常确定它会在您的程序中起作用,因为这是我以前做过的事情。您将使用系统的桌面类来启动程序的打开。程序看起来像这样

public static void restart()
{
Desktop desktop = Desktop.getDesktop();
    try {
        desktop.open(new File("name_of_your_jarfile.jar").getCanonicalFile());
// desktop.open(new File("name_of_your_exe.exe").getCanonicalFile());//for exe
        } catch (IOException e) {
            //if this happen try looking whether the name of your jar file is correct
            e.printStackTrace();
        }
}
private static void game() //game method
{
    //...

   int play = JOptionPane.showOptionDialog(null
                   ,"End"
                   , "Do you want to play again?"
                   , JOptionPane.PLAIN_MESSAGE
                   ,JOptionPane.DEFAULT_OPTION
                   , null
                   , again
                   , again[1]);
   //end of game
    if (play == 0)
        System.exit(0);//exit
    else
         /* what do I put here to restart the program in the same 
            after pressing the No button on the JOptionPane???    */        
         System.out.println("Service not available");  
       restart();//this will reopen your jar/exe file
       System.exit(0);
}
© www.soinside.com 2019 - 2024. All rights reserved.