【问题标题】:JAVA: Running method without running the next line until the method endJAVA:运行方法而不运行下一行,直到方法结束
【发布时间】:2014-09-07 03:56:39
【问题描述】:
int x = Integer.parseInt(JOptionPane.showInputDialog("INPUT 1 FOR GAME, ANY FOR EXIT"));
while(x == 1){
    new PokelanzBattle();
    x = Integer.parseInt(JOptionPane.showInputDialog("INPUT 1 FOR GAME, ANY FOR EXIT"));
}

在这段代码中,假设您输入了 1,while 将运行。 在 while 循环中,我只想运行 PokelanzBattle() 方法,但在运行它之后,下一行也将被读取并运行。

我怎样才能只运行方法,并且在我处理 gui 后(gui/方法会自动处理,其中包含一些方法),下一行将运行,即输入对话框。

【问题讨论】:

  • PokelanzBattle() 不是方法,而是构造函数。
  • 在构造函数调用返回之前,它已经不会运行while 循环体的第二行。在运行第二行之前你还想做什么?
  • @Dici 是的,这是一个构造函数,我的错。

标签: java loops methods while-loop joptionpane


【解决方案1】:

更好的方法是在游戏真正结束时要求重新游戏。类似:

int x = Integer.parseInt(JOptionPane.showInputDialog("INPUT 1 FOR GAME, ANY FOR EXIT"));
if(x == 1)
   {
    new PokelanzBattle();
   }

当游戏结束时:

int x = Integer.parseInt(JOptionPane.showInputDialog("GAME OVER!!! INPUT 1 FOR GAME, ANY FOR EXIT"));
if(x == 1){//....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2012-04-16
    • 1970-01-01
    相关资源
    最近更新 更多