Mastermind 游戏 if 语句问题
Mastermind game if statement problems
我正在努力做到这一点,所以当我的两个或多个 if 语句起作用时,它们不会同时打印,而只是说两个在正确的位置或更多它更有效。我不确定我是否应该做更多的 if 语句,如果我应该使用另一个语句或 forloop。
//If Statements
if (Gameboard[0] == Secret_code[0]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[1] == Secret_code[1]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[2] == Secret_code[2]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[3] == Secret_code[3]) {
System.out.println ("You have one in the correct spot");
}
}
}
您可以遍历 Gameboard[x] == Secret_code[x]
个检查并在最后打印总数。
编辑:本着促进学习的精神删除了代码。
解决方案"In words"
与其仅仅检查每个点的代码是否正确然后立即打印,不如考虑创建一个变量来计算挂钩正确的次数。然后不要打印任何东西,直到你知道这个变量的值已经考虑了 Secret_code
的所有元素(一个好方法("right" 方法))这样做是循环通过 Secret_Code
数组并在每次代码正确时使用新变量进行计数。
最后,使用包含正确数量信息的变量将消息打印给用户。
我不会包含示例代码,因此您一定要自己实施和理解它:-)
您可以创建复合条件
if(cond1 || cond2 ||.....){print};
Java 的本质是,一旦遇到第一个真条件,进一步的评估就会停止并执行打印语句。这只是众多解决方案中的一种。但是如果你的数组索引很大,我不推荐我的解决方案,因为复合条件最多应该只有 3 或 4,恕我直言。
if (Gameboard[0] == Secret_code[0]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[1] == Secret_code[1]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[2] == Secret_code[2]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[3] == Secret_code[3]) {
System.out.println ("You have one in the correct spot");
}
应该改为类似于
的东西
private static final String[] numbers = {zero, one, two, three, four};
int correctCount = 0;
for(int i = 0; i < /*4*/ GameBoard.length; i++) {
if(Gameboard[i] == Secret_code[i]) {
currentCount++;
}
}
System.out.println("You have " + numbers[currentCount] + " in the correct spot.");
我正在努力做到这一点,所以当我的两个或多个 if 语句起作用时,它们不会同时打印,而只是说两个在正确的位置或更多它更有效。我不确定我是否应该做更多的 if 语句,如果我应该使用另一个语句或 forloop。
//If Statements
if (Gameboard[0] == Secret_code[0]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[1] == Secret_code[1]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[2] == Secret_code[2]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[3] == Secret_code[3]) {
System.out.println ("You have one in the correct spot");
}
}
}
您可以遍历 Gameboard[x] == Secret_code[x]
个检查并在最后打印总数。
编辑:本着促进学习的精神删除了代码。
解决方案"In words"
与其仅仅检查每个点的代码是否正确然后立即打印,不如考虑创建一个变量来计算挂钩正确的次数。然后不要打印任何东西,直到你知道这个变量的值已经考虑了 Secret_code
的所有元素(一个好方法("right" 方法))这样做是循环通过 Secret_Code
数组并在每次代码正确时使用新变量进行计数。
最后,使用包含正确数量信息的变量将消息打印给用户。
我不会包含示例代码,因此您一定要自己实施和理解它:-)
您可以创建复合条件
if(cond1 || cond2 ||.....){print};
Java 的本质是,一旦遇到第一个真条件,进一步的评估就会停止并执行打印语句。这只是众多解决方案中的一种。但是如果你的数组索引很大,我不推荐我的解决方案,因为复合条件最多应该只有 3 或 4,恕我直言。
if (Gameboard[0] == Secret_code[0]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[1] == Secret_code[1]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[2] == Secret_code[2]) {
System.out.println ("You have one in the correct spot");
}
if (Gameboard[3] == Secret_code[3]) {
System.out.println ("You have one in the correct spot");
}
应该改为类似于
的东西private static final String[] numbers = {zero, one, two, three, four};
int correctCount = 0;
for(int i = 0; i < /*4*/ GameBoard.length; i++) {
if(Gameboard[i] == Secret_code[i]) {
currentCount++;
}
}
System.out.println("You have " + numbers[currentCount] + " in the correct spot.");