對於用戶輸入true或false的第二個選擇部分。如果他們鍵入「true」,則會發生一件事,如果他們鍵入「false」,則會發生另一件事。這部分沒有問題。如何根據用戶輸入使某個代碼塊運行?
我的問題在於第三部分。我希望發生在第二個選擇部分的相同的事情發生,但是,我也希望第二個選擇部分影響它。例如,如果用戶鍵入的兩板「真」,然後逃離發生這種情況:
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");
}
如果他們鍵入船上逃離「造假」真的,那麼別的東西會發生。
//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");
}
你需要提供更多關於你想要做什麼的更多信息。這是很模糊的。 – Carcigenicate
真的不清楚你在問什麼,但請注意,你已經提供了很多代碼,並且沒有解釋,也沒有試圖將它縮小到[mcve]的跡象。我會說'如果(board = true)'看起來明顯不禮貌 - 你是否知道'='是賦值操作符,而'=='是用於相等性測試的? –
當我運行它時,真實部分起作用,但不起作用。此外,您董事會部分工作正常。 –