布尔值不会 return false
boolean will not return false
import java.util.Random;
import java.util.Scanner;
public class HiLo {
/**
* Nick Jones
* 2/10/2015
* High or Low
*/
public static boolean high() {
int x;
boolean answer;
Random randomGenerator = new Random();
x = randomGenerator.nextInt(9 - 1) + 1;
System.out.println("number is " + x);
if (x > 6 && x < 14) {
System.out.println("You win!");
answer = true;
return answer;
} else {
System.out.println("You lose!");
answer = false;
return answer;
}
}
public static boolean low() {
int x;
boolean answer;
Random randomGenerator = new Random();
x = randomGenerator.nextInt(9 - 1) + 1;
System.out.println("number is " + x);
if (x > 0 && x < 7) {
System.out.println("You win!");
answer = true;
return answer;
} else {
System.out.println("You lose!");
answer = false;
return answer;
}
}
public static void main(String[] args) {
int points = 1000;
int risk;
int guess;
boolean answer;
int again;
do {
System.out.println("you have " + points + " points.");
Scanner input = new Scanner (System.in);
System.out.println ("Input number of points to risk: ");
risk = input.nextInt();
System.out.println ("predict <1-high, 0-low>: ");
guess = input.nextInt();
if (guess == 1) {
answer = high();
} if (guess == 0) {
answer = low();
}
if (answer = true) {
points = points + (risk*2);
**} if (answer = false) {
points = points - risk;**
}
System.out.println("You have " + points + " points.");
System.out.println("play again?<yes-1, no-0> ");
again = input.nextInt();
} while (again == 1);
}
}
这个程序的目的是从得分为 1000 分的玩家开始,然后随机生成一个数字,他们选择他们的分数到 'risk',然后选择高或低(低 - 1- 6. 高 - 8-13) 如果他们的猜测是正确的,他们的风险就会加倍并加回到他们的分数中。如果不正确,则从分数中减去风险。我的布尔语句似乎正在从
停止程序
if (answer = false) {
points = points - risk;
这部分,所以我的布尔值永远不会 returns false 是我认为我的问题所在。因为当 运行 它只允许玩家赢,永远不会输,它会输出 'you lose' 但仍然添加点数,就好像他们赢了一样。
您正在使用赋值运算符 =
,因此 answer
始终是 true
。相等的比较运算符是 ==
,因为您已经在代码的其他地方使用过。但是 answer
已经是一个布尔值。不需要用==
来比较;只是使用它。变化
if (answer = true) {
points = points + (risk*2);
} if (answer = false) {
points = points - risk;
}
至
if (answer) {
points = points + (risk*2);
} else {
points = points - risk;
}
import java.util.Random;
import java.util.Scanner;
public class HiLo {
/**
* Nick Jones
* 2/10/2015
* High or Low
*/
public static boolean high() {
int x;
boolean answer;
Random randomGenerator = new Random();
x = randomGenerator.nextInt(9 - 1) + 1;
System.out.println("number is " + x);
if (x > 6 && x < 14) {
System.out.println("You win!");
answer = true;
return answer;
} else {
System.out.println("You lose!");
answer = false;
return answer;
}
}
public static boolean low() {
int x;
boolean answer;
Random randomGenerator = new Random();
x = randomGenerator.nextInt(9 - 1) + 1;
System.out.println("number is " + x);
if (x > 0 && x < 7) {
System.out.println("You win!");
answer = true;
return answer;
} else {
System.out.println("You lose!");
answer = false;
return answer;
}
}
public static void main(String[] args) {
int points = 1000;
int risk;
int guess;
boolean answer;
int again;
do {
System.out.println("you have " + points + " points.");
Scanner input = new Scanner (System.in);
System.out.println ("Input number of points to risk: ");
risk = input.nextInt();
System.out.println ("predict <1-high, 0-low>: ");
guess = input.nextInt();
if (guess == 1) {
answer = high();
} if (guess == 0) {
answer = low();
}
if (answer = true) {
points = points + (risk*2);
**} if (answer = false) {
points = points - risk;**
}
System.out.println("You have " + points + " points.");
System.out.println("play again?<yes-1, no-0> ");
again = input.nextInt();
} while (again == 1);
}
}
这个程序的目的是从得分为 1000 分的玩家开始,然后随机生成一个数字,他们选择他们的分数到 'risk',然后选择高或低(低 - 1- 6. 高 - 8-13) 如果他们的猜测是正确的,他们的风险就会加倍并加回到他们的分数中。如果不正确,则从分数中减去风险。我的布尔语句似乎正在从
停止程序if (answer = false) {
points = points - risk;
这部分,所以我的布尔值永远不会 returns false 是我认为我的问题所在。因为当 运行 它只允许玩家赢,永远不会输,它会输出 'you lose' 但仍然添加点数,就好像他们赢了一样。
您正在使用赋值运算符 =
,因此 answer
始终是 true
。相等的比较运算符是 ==
,因为您已经在代码的其他地方使用过。但是 answer
已经是一个布尔值。不需要用==
来比较;只是使用它。变化
if (answer = true) {
points = points + (risk*2);
} if (answer = false) {
points = points - risk;
}
至
if (answer) {
points = points + (risk*2);
} else {
points = points - risk;
}