对于第二个选择部分,用户输入true或false。如果他们输入" true"有一件事发生了,如果他们输入" false"另一件事发生了。这部分很好。
我的问题在于第三选择部分。我想要在第二选择部分发生同样的事情,但是,我也想要第二个选择部分来影响它。例如,如果用户键入" true"对于董事会和逃离,然后发生这种情况:
if(flee == true ) {
System.out.println("As you try to flee the Destroyer opens fire and you barely escape intact.");
System.out.println("This is the current level of your most vital systems: ");
System.out.println("Shield Level: 0%");
System.out.println("Ammunition Level: 0%");
System.out.println("Fuel Level: 5%");
System.out.println("Oxygen Level: 10%");
System.out.println("After your close call you go to the nearest heavy repair depot.");
if(board = true) {
System.out.println("Now repairing shield");
for (Shield = 0; Shield <= 100; Shield += 50) {
System.out.println(Shield + "%");
}
System.out.println("Shields repaired");
System.out.println("Now replenishing ammunition");
for (Ammunition = 0; Ammunition <= 100; Ammunition += 50) {
System.out.println(Ammunition + "%");
}
System.out.println("Ammunition replenished");
System.out.println("Now refueling");
for (Fuel = 5; Fuel <= 100; Fuel += 5) {
System.out.println(Fuel + "%");
}
System.out.println("Fueling complete");
System.out.println("Now replenishing oxygen levels");
for (Oxygen = 10; Oxygen <= 100; Oxygen += 10) {
System.out.println(Oxygen + "%");
}
System.out.println("Oxygen replenished");
}
如果他们键入&#34; false&#34;对于逃离和真实的董事会然后会发生其他事情。
//2nd choice
System.out.println("With all of your vital systems back to 100% you head to planet X34.");
System.out.println("As you approach X34 a massive cargo ship appears and you receive an invitation to dock and come aboard the ship.");
System.out.println("Do you board? True or False");
Scanner scanner1 = new Scanner(System.in);
boolean board = scanner1.nextBoolean();
//board yes
if(board = true & board != false) {
System.out.println("After docking and boarding the cargo ship the captain of the ship comes up to you");
System.out.println("Captain: Welcome aboard I am Captain Skrull, here is card which allows for the fastest repairs available as a token of our friendship");
System.out.println("You: Thank you so much");
System.out.println("Skrull: You're welcome, now let us feast");
}
//board no
else {
System.out.println("After declining the invitation the cargo ship locks its weapons on you because you didn't accept");
System.out.println("The cargo ship and you battle until your ship is critically damaged and must flee for urgent repairs.");
System.out.println("This is the current level of your most vital systems: ");
System.out.println("Shield Level: 0%");
System.out.println("Ammunition Level: 0%");
System.out.println("Fuel Level: 10%");
System.out.println("Oxygen Level: 15%");
System.out.println("After barley escaping you head to the nearest repair depot to fix your ship.");
System.out.println("Now repairing shield");
for (Shield = 0; Shield <= 100; Shield += 10) {
System.out.println(Shield + "%");
}
System.out.println("Shields repaired");
System.out.println("Now replenishing ammunition");
for (Ammunition = 0; Ammunition <= 100; Ammunition += 5) {
System.out.println(Ammunition + "%");
}
System.out.println("Ammunition replenished");
System.out.println("ERROR: DUE TO EXTENSIVE DAMGE FUEL TANK HAS BEEN DAMAGED. YOU CAN NOW HOLD ONLY 90% OF MAX FUEL.");
System.out.println("Now refueling");
for (Fuel = 10; Fuel <= 90; Fuel += 5) {
System.out.println(Fuel + "%");
}
System.out.println("Fueling complete");
System.out.println("Now replenishing oxygen levels");
for (Oxygen = 15; Oxygen <= 100; Oxygen += 5) {
System.out.println(Oxygen + "%");
}
System.out.println("Oxygen replenished");
}
//3rd choice
System.out.println("After your encounter with Captain Skrull you head to the are between the planets X70, Z70, and Y70 also known as the Bermuda triangle of space to investigate. ");
System.out.println("As you approach the Bermuda triangle of space the feared warship called the Destroyer appears. Your scanners show that multiple weapons are locked on to you.");
System.out.println("Do you flee? (True or False)");
Scanner scanner2 = new Scanner(System.in);
boolean pop = scanner2.nextBoolean();
boolean flee = Boolean.valueOf(pop);
//flee yes
if(flee == true ) {
System.out.println("As you try to flee the Destroyer opens fire and you barely escape intact.");
System.out.println("This is the current level of your most vital systems: ");
System.out.println("Shield Level: 0%");
System.out.println("Ammunition Level: 0%");
System.out.println("Fuel Level: 5%");
System.out.println("Oxygen Level: 10%");
System.out.println("After your close call you go to the nearest heavy repair depot.");
if(board = true) {
System.out.println("Now repairing shield");
for (Shield = 0; Shield <= 100; Shield += 50) {
System.out.println(Shield + "%");
}
System.out.println("Shields repaired");
System.out.println("Now replenishing ammunition");
for (Ammunition = 0; Ammunition <= 100; Ammunition += 50) {
System.out.println(Ammunition + "%");
}
System.out.println("Ammunition replenished");
System.out.println("Now refueling");
for (Fuel = 5; Fuel <= 100; Fuel += 5) {
System.out.println(Fuel + "%");
}
System.out.println("Fueling complete");
System.out.println("Now replenishing oxygen levels");
for (Oxygen = 10; Oxygen <= 100; Oxygen += 10) {
System.out.println(Oxygen + "%");
}
System.out.println("Oxygen replenished");
}
else if (board = false){
System.out.println("Now repairing shield");
for (Shield = 0; Shield <= 100; Shield += 10) {
System.out.println(Shield + "%");
}
System.out.println("Shields repaired");
System.out.println("Now replenishing ammunition");
for (Ammunition = 0; Ammunition <= 100; Ammunition += 5) {
System.out.println(Ammunition + "%");
}
System.out.println("Ammunition replenished");
System.out.println("Now refueling");
for (Fuel = 5; Fuel <= 90; Fuel += 5) {
System.out.println(Fuel + "%");
}
System.out.println("Fueling complete");
System.out.println("Now replenishing oxygen levels");
for (Oxygen = 10; Oxygen <= 100; Oxygen += 5) {
System.out.println(Oxygen + "%");
}
System.out.println("Oxygen replenished");
}
else {
System.out.println("User Error");
}
}
else if (flee = false){
System.out.println("Both you and the Destroyer open fire and engage in fierce battle which results in your oxygen levels to be depleted and the Destroyers weapon system to get destroyed");
System.out.println("This is the current level of your most vital systems: ");
System.out.println("Shield Level: 20%");
System.out.println("Ammunition Level: 0%");
System.out.println("Fuel Level: 25%");
System.out.println("Oxygen Level: 0%");
System.out.println("After the battle ends you head to the nearest repair depot and medical center for repairs and treatment for longterm low oxygen levels");
if (board = true) {
System.out.println("Now repairing shield");
for (Shield = 20; Shield <= 100; Shield += 20) {
System.out.println(Shield + "%");
}
System.out.println("Shields repaired");
System.out.println("Now replenishing ammunition");
for (Ammunition = 0; Ammunition <= 100; Ammunition += 50) {
System.out.println(Ammunition + "%");
}
System.out.println("Ammunition replenished");
System.out.println("Now refueling");
for (Fuel = 25; Fuel <= 100; Fuel += 25) {
System.out.println(Fuel + "%");
}
System.out.println("Fueling complete");
System.out.println("Now replenishing oxygen levels");
for (Oxygen = 0; Oxygen <= 100; Oxygen += 50) {
System.out.println(Oxygen + "%");
}
System.out.println("Oxygen replenished");
}
else if (board = false){
System.out.println("Now repairing shield");
for (Shield = 20; Shield <= 100; Shield += 10) {
System.out.println(Shield + "%");
}
System.out.println("Shields repaired");
System.out.println("Now replenishing ammunition");
for (Ammunition = 0; Ammunition <= 100; Ammunition += 5) {
System.out.println(Ammunition + "%");
}
System.out.println("Ammunition replenished");
System.out.println("Now refueling");
for (Fuel = 25; Fuel <= 90; Fuel += 5) {
System.out.println(Fuel + "%");
}
System.out.println("Fueling complete");
System.out.println("Now replenishing oxygen levels");
for (Oxygen = 0; Oxygen <= 100; Oxygen += 5) {
System.out.println(Oxygen + "%");
}
System.out.println("Oxygen replenished");
}
else {
System.out.println("User Error");
}
}
else {
System.out.println("User Error");
}
答案 0 :(得分:3)
您的if
语句将无法满足您的期望。让我们举个例子来看看原因:
if(board = true & board != false)
这有很多问题:
true
和false
进行比较。这样做被认为是不好的风格。 if (board)
完全等同于if (board == true)
。board = true
是作业,而不是比较。你实际上在几个地方都有这个错误。这是if
语句无法按预期运行的主要原因之一:if (board = true)
不可能为false,因此else
语句可以可能会跑。如果您混淆了=
和==
(有几个),请仔细检查您的代码。if (board == true && board != false)
是多余的,可能代表对boolean
值的工作方式的误解。如果是(board == true)
,那么当然 board != false
。想一想board == true && board == false
的意思;在什么条件下这样的可能是真的?请注意,根据定义,boolean
值 true
或 false(但不是两者,当然也不是第三个值);只有两种可能的选择。我绝对鼓励您查看比较如何在Java中工作以及查看this rather excellent article on debugging。您特别希望专注于学习如何以及在何处设置断点(即IDE将暂停执行并允许您检查程序状态的点)以及如何单步执行代码(即,一次运行一行程序,以便您可以通过调试器看到每行代码的效果。
答案 1 :(得分:2)
欢迎编程!
在Java中,=
符号或运算符用于赋值,这意味着=
右侧的任何内容都将保存到其左侧的任何内容中。< / p>
您想要的标志是==
。 ==
符号是一个比较符号,主要用于布尔值(包含true
或false
的变量)
根据你的评论,我会说你现在应该通过在他们自己的代码块中包含登机后的部分来避免记住这一点。这样,只有当玩家决定登船时,您才会执行逃离检查。
例如:
// Prompt user to see if they want to board, capture input.
if (board) {
adventurePostBoarding();
}
并在下面定义您的方法:
private final void adventurePostBoarding() {
// TODO code here
}
将来,请记住使用true
而不是false
来比较==
和=
值。