java - 如何在结束前停止方法
问题描述
我想知道如何停止读取方法中的代码,因为我有一个带有多个 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() 的东西,但我不知道这是否可能/我可以使用什么语法!谢谢
解决方案
您的问题中没有循环,但您询问的是控制流。我想你想要if-then-else 语句。或者你可以使用return
语句。一般来说,Java 教程是学习这类东西的绝佳资源。
推荐阅读
- model-view-controller - 如何获取绑定到 MVC 中复杂对象列表的属性的复选框输入值?
- c++ - 如何摆脱 OpenCv 的 Bad argument 错误
- javascript - Unexpected token exporting async function
- mysql - MYSQL 使用 AND 和通配符
- java - 使用谓词作为单元测试的参数
- symfony - 自定义操作上的 api-platform 不需要的字段更新
- umbraco - 导入 Umbraco 属性编辑器
- c++ - 将顺序容器“移动”到指针
- php - 在字符串 PHP 数组上添加花括号
- html - 我无法渲染反应