以下是说明:
/*
* 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";
}
它显然不起作用,我只是感觉到这是因为我使用了一个while循环。如何在不使用if语句的情况下做出决定?
答案 0 :(得分:3)
如果您想要一个可读解决方案,那么您几乎可以直接将问题描述转换为代码。
/**
* 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);
}
答案 1 :(得分:1)
可以尝试这样的事情。我没有检查确切的条件。这更像是一个示例程序。
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;
}
编辑::人们指出使用boolean作为返回类型而不是String。正如我已经提到的那样,目的是展示如何替换if-else条件而不是编写整个程序。所以请考虑。
答案 2 :(得分:1)
怎么样
public static boolean isGameOver(int score, int lives, int level) {
return lives<1 || (level<3 && score<1000*level) || (score<3000 && level>2);
}
测试此
isGameOver(900,1,1) // true
isGameOver(1900,1,1) // false
isGameOver(1900,1,2) // true
isGameOver(2900,1,2) // false
isGameOver(3000,1,5) // false
isGameOver(3000,0,5) // true
如果至少有一个条件为真,则三个条件或 -ed将一起返回true。我们按顺序检查
lives<1
)?如果满足以下任何条件,我们将返回true。否则我们会返回false。
答案 3 :(得分:0)
这个怎么样?
var contractor = new Contractor()
{
UserName = contractorModel.UserName,
Email = contractorModel.Email,
EmailConfirmed = true,
SecurityStamp = Guid.NewGuid().ToString()
};
var result = await UserManager.CreateAsync(contractor,"Passw0rd!");
答案 4 :(得分:0)
检查出来
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);
}
答案 5 :(得分:-1)
您可以使用允许来自java 1.7的字符串值的switch语句。您可以计算表达式并将其分配给字符串变量,然后在switch中使用该变量。
注意:您无法直接在切换案例中放置表达式。
答案 6 :(得分:-1)
首先,您需要了解while
语句中逻辑出错的位置。声明:
while (level == 1 && level < 1001){
...
}
while (level == 2 && level < 2001){
...
}
while (level == 3 && level < 3001){
...
}
总是如此,因此默认情况下您将始终返回"true"
。您需要在第二个条件中评估score
,而不是level
。此外,路线指定您在此方法中返回boolean
而不是String
,因此您的返回类型必须为boolean
。
您可以使用return语句来评估布尔值,您不一定需要if
语句来证明某些事情是否属实。例如,return 1 == 0;
将返回false。在你的情况下:
public static boolean isGameOver(int score, int lives, int level){
while (level == 1 && lives > 0)
return score < 1000; // If score is greater than 1000, returns false, else true
while (level == 2 && lives > 0)
return score < 2000; // If score is greater than 2000, returns false, else true
while (level >= 3 && lives > 0)
return score < 3000; // If score is greater than 3000, returns false, else true
return true; // This accounts for if lives are not greater than 0
}
此方法考虑了玩家必须同时具有多个生命和某个分数以便可能返回错误的级别和事实。在这种情况下,使用return
语句作为boolean
求值程序会否定您对if
语句的需求。
答案 7 :(得分:-1)
我认为你需要这样的东西
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
答案 8 :(得分:-2)
public static String isGameOver(int score, int lives, int level) {
return level*1000 >= score;
}
答案 9 :(得分:-3)
我会把它编码为:
const int& dynamic_array::operator[](unsigned int i) const