【问题标题】:How to stop a method before end如何在结束前停止方法
【发布时间】:2019-02-25 16:15:56
【问题描述】:

我想知道如何停止读取方法中的代码,因为我有一个带有多个 if() 的方法,如果第一个完成,第二个现在也变得可读,等等:

 public void putPlayerInLadderBord() {


    if(player1Score < 0) {
        player1Score = currentPlayerScore;
        player1Name = currentPlayerName;
        player1.setText(player1Name + " - Score : " + player1Score);
    }
    if (player1Score >= 0 && player2Score < 0) {
        player2Score = currentPlayerScore;
        player2Name = currentPlayerName;
        player2.setText(player2Name + " - Score : " + player2Score);
    }
    if (player1Score >= 0 && player2Score >= 0 && player3Score < 0) {
        player3Score = currentPlayerScore;
        player3Name = currentPlayerName;
        player3.setText(player3Name + " - Score : " + player3Score);
    }
    if (player1Score >= 0 && player2Score >= 0 && player3Score >= 0 && player4Score < 0) {
        player4Score = currentPlayerScore;
        player4Name = currentPlayerName;
        player4.setText(player4Name + " - Score : " + player4Score);
    }
    if (player1Score >= 0 && player2Score >= 0 && player3Score >= 0 && player4Score >= 0 && player5Score < 0) {
        player5Score = currentPlayerScore;
        player5Name = currentPlayerName;
        player5.setText(player5Name + " - Score : " + player5Score);
    }
}

结果,当玩家完成游戏时,他的分数+名字会被放入每个 LadderBord 的情况下 ^^ 我想在每个 if 之后添加诸如 stop() 之类的东西,但我不知道这是否可能/我可以使用什么语法! 谢谢

【问题讨论】:

  • 只需使用return;
  • 您好,请使用 return 退出 if 条件,如下语句:return;
  • 对于上面的例子,你应该使用 else if 语句而不是连续的 if 语句。

标签: java android loops methods


【解决方案1】:

您的问题中没有循环,但您询问的是控制流。我想你想要if-then-else statement。或者你可以使用the return statement。一般来说,the Java Tutorials 是学习这类东西的绝佳资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 2016-04-30
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多