2013-11-25 94 views
1

我目前正在研究java版本的掃雷。當用戶點擊地雷或贏得比賽時,我想要實現一個「是」,「否」的選項面板,它將用作「您想再次玩」的功能。實現no很簡單,但即使在程序再次運行時執行yes選項時仍然存在問題,但在我的情況下,它只是運行程序的新實例並且不關閉舊實例。建議?謝謝。再次運行程序

這裏就是用戶發現所有的礦山和勝代碼:

if (check == nomines) 
      { 
       endtime = System.nanoTime(); 
       Component temporaryLostComponent = null; 
       JOptionPane.showMessageDialog(temporaryLostComponent, "Congratulations, you won! It took you " + (int)((endtime-starttime)/1000000000) + " seconds!"); 

       int p = JOptionPane.showConfirmDialog(this, "Play again?","Minefield",JOptionPane.YES_NO_OPTION); 
       if (p == 1) 
       { 
        System.exit(0); 
       } 
       else 
       { 
        new Minefield(); 
       } 

      } 
     } 
+2

爲什麼不你只需重新設置比賽場地並隨機分配礦場位置? – thegrinner

+0

您還需要處理當前正在運行的實例。我不知道如何實施雷區,因爲你沒有顯示,但如果它是一個JFrame,你可以使用.dispose() –

+0

運行任何構造函數/方法你在開始初始化所有的東西。 – Cruncher

回答

2

使用做而循環,它使你輕鬆的工作..

do{ 

    // your all operations... 

// at last prompt yes or no ? 
} while(choice.equalsIgnoreCase("yes")); 
相關問題