Java 无if声明的决定
以下是说明:Java 无if声明的决定,java,Java,以下是说明: /* * Write a method called isGameOver. The method should have three parameters, * all integers and in this order: the player's score, the number of lives * the player has remaining, the game level. The method should return true * if the
/*
* Write a method called isGameOver. The method should have three parameters,
* all integers and in this order: the player's score, the number of lives
* the player has remaining, the game level. The method should return true
* if the game is over and false otherwise, according to the following game
* rule. The game continues only when the player has at least one life
* remaining and one of these conditions is met:
* --The player is on level 1 and his/her score at least 1000.
* --The player is on level 2 and his/her score at least 2000.
* --The player is on any level and his/her score at least 3000.
*
* For example, the method call isGameOver(1500, 1, 2) should return true.
*
* DO NOT USE AN IF STATEMENT
这是我的代码:
public static String isGameOver(int score, int lives, int level) {
while (level == 1 && level < 1001)
return "false";
while (level == 2 && level < 2001)
return "false";
while (level == 3 && level < 3001)
return "false";
return "true";
}
公共静态字符串isGameOver(整数分数、整数寿命、整数级别){
而(级别==1&&level<1001)
返回“false”;
而(级别==2和级别<2001)
返回“false”;
而(级别==3&&level<3001)
返回“false”;
返回“真”;
}
它显然不起作用,我只是有这种感觉,因为我使用了一个while循环。如果不使用if语句,我如何做出决定?可以尝试类似的方法。我还没有检查确切的情况。这更像是一个示例程序
public static String isGameOver(int score, int lives, int level) {
String result = "false";
switch (level){
case 1:
result= score>1000 ? "true" : "false";
break;
case 2:
result= score>2000 ? "true" : "false";
break;
default:
result= score>3000 ? "true" : "false";
return result;
}
编辑::人们指出使用布尔值作为返回类型而不是字符串。正如我已经指出的,目的是展示如何替换if-else条件,而不是编写整个程序。所以请考虑。< p>可以使用允许java 1.7的字符串值的开关语句。您可以计算表达式并将其分配给字符串变量,然后在开关中使用该变量 注意:您不能直接将表达式放在开关盒中。这如何
public static boolean isGameOver(int score, int lives, int level) {
return ((level == 1 && score < 1001)
|| (level == 2 && score < 2001)
|| (level == 3 && score < 3001));
}
publicstaticboolearnisgameover(整数分数、整数寿命、整数级别){
返回((级别==1和分数<1001)
||(等级==2,分数<2001)
||(等级==3,分数<3001);
}
首先,您需要了解while语句中的逻辑哪里出错了。声明:
while (level == 1 && level < 1001){
...
}
while (level == 2 && level < 2001){
...
}
while (level == 3 && level < 3001){
...
}
该方法考虑了玩家必须拥有一次以上的生命和一定的分数才能返回false的等级和事实。使用return
语句作为boolean
求值器,在这种情况下,您不需要使用if
语句。我将其编码为:
public static boolean isGameOver(int score, int lives, int level) {
while(lives > 0){
switch (level) {
case 1:
return (score > 1000) ? false : true ;
case 2:
return (score > 2000) ? false : true ;
case 3:
return (score > 3000) ? false : true ;
}
return false;
}
}
怎么样
public static boolean isGameOver(int score, int lives, int level) {
return lives<1 || (level<3 && score<1000*level) || (score<3000 && level>2);
}
如果至少有一个条件为真,则这三个条件或ed一起将返回真。我们按顺序检查
lifes如果你想要一个可读的解决方案,那么你几乎可以直接将问题描述翻译成代码
/**
* The game continues only when the player...
**/
private static boolean gameContinues(int score, int lives, int level) {
// has at least one life
boolean stillAlive = lives >= 1;
// is on level 1 and his/her score at least 1000
boolean cond1 = (level == 1 && score >= 1000);
// is on level 2 and his/her score at least 2000
boolean cond2 = (level == 2 && score >= 2000);
// is on any level and his/her score at least 3000
boolean cond3 = score >= 3000;
// return true if has at least one life remaining and one of the conditions is met
return stillAlive && (cond1 || cond2 || cond3);
}
// The function you want just returns the inverse of what is defined
public static boolean isGameOver(int score, int lives, int level) {
return !gameContinues(score, lives, level);
}
看看这个
public boolean isGameOver(int score, int lives, int level) {
boolean hasLife = (lives >= 1);
boolean condition1 = (hasLife && (level == 1) && (score >= 1000));
boolean condition2 = (hasLife && (level == 2) && (score >= 2000));
boolean condition3 = (hasLife && score >= 3000);
return !(condition1 || condition2 || condition3);
}
我想你需要这样的东西
import java.util.*;
class FirstApp {
public static void main(String[] args) {
System.out.println(String.valueOf(isGameOver(2800,0,1)));
System.out.println(String.valueOf(isGameOver(1800,1,2)));
System.out.println(String.valueOf(isGameOver(2800,1,3)));
System.out.println(String.valueOf(isGameOver(1000,1,1)));
System.out.println(String.valueOf(isGameOver(2800,1,2)));
System.out.println(String.valueOf(isGameOver(3800,1,1)));
System.out.println(String.valueOf(isGameOver(3800,1,2)));
System.out.println(String.valueOf(isGameOver(3800,1,3)));
}
public static boolean isGameOver(int score, int lives, int level) {
if (lives < 1) return true;
HashMap<Integer,Integer> levelScores = new HashMap();
levelScores.put(1,1000);
levelScores.put(2,2000);
levelScores.put(3,3000);
return (levelScores.get(level) > score)? true :false;
}
}
提示:&
和|
使用短路求值。此外,此函数似乎总是返回true
值。请重新考虑返回类型。您是否打算测试分数
?如果级别等于1,则低于1001。我认为您的意思是分数<1001
上面的大多数注释都解决了这个问题lem与您的支票,但您也没有使用生命变量。此外,请不要只说“它不工作”。请告诉我们它在做什么,以及为什么这不是您所期望的。问题是:不要使用IF语句
不要使用IF语句。尽管它很愚蠢,但这是问题的一部分。可以对每种情况返回分数>值
。@cricket\u 007:它将返回一个布尔值,我们想返回字符串。您忘记了另一个条件ion:只有当玩家至少还剩一条生命,并且满足以下条件之一时,游戏才会继续:我认为函数不应该返回字符串。您可以只返回布尔值。只需将整个值取反。函数应该返回一个布尔值
。这些语句中的逻辑也不正确。因为级别
总是小于这些数字,无论玩家处于哪个级别,它都将返回true。这不会返回正确的值。isGameOver(1500,1,2)
应该是真的,这返回假。它也不使用生命。在这种情况下,你为什么需要做一段时间?这是现有答案的副本,你可以做返回分数>1000;
在每种情况下,实际上你都想返回分数<1000
,因为值真的意味着游戏已经结束。嘿,谢谢你拜托!我真的很感激有人告诉我我做错了什么,这样我以后就可以避免类似的错误。当然,这就是学习的意义所在。这段代码应该对你很有用。这对超过3级的人不起作用,例如isGameOver(3001,1,5)will return True不考虑生命我太累了,看不出布尔逻辑是否等同于我的答案:)@cricket_007是的,不同于我组合1级和2级的方式,而你将它们分开。当然,你把问题倒过来测试游戏是否仍然是第一位的。
public static String isGameOver(int score, int lives, int level) {
return level*1000 >= score;
}
public boolean isGameOver(int score, int lives, int level) {
boolean hasLife = (lives >= 1);
boolean condition1 = (hasLife && (level == 1) && (score >= 1000));
boolean condition2 = (hasLife && (level == 2) && (score >= 2000));
boolean condition3 = (hasLife && score >= 3000);
return !(condition1 || condition2 || condition3);
}
import java.util.*;
class FirstApp {
public static void main(String[] args) {
System.out.println(String.valueOf(isGameOver(2800,0,1)));
System.out.println(String.valueOf(isGameOver(1800,1,2)));
System.out.println(String.valueOf(isGameOver(2800,1,3)));
System.out.println(String.valueOf(isGameOver(1000,1,1)));
System.out.println(String.valueOf(isGameOver(2800,1,2)));
System.out.println(String.valueOf(isGameOver(3800,1,1)));
System.out.println(String.valueOf(isGameOver(3800,1,2)));
System.out.println(String.valueOf(isGameOver(3800,1,3)));
}
public static boolean isGameOver(int score, int lives, int level) {
if (lives < 1) return true;
HashMap<Integer,Integer> levelScores = new HashMap();
levelScores.put(1,1000);
levelScores.put(2,2000);
levelScores.put(3,3000);
return (levelScores.get(level) > score)? true :false;
}
}
true
true
true
false
false
false
false
false