2017-07-18 59 views
-8

對於用戶輸入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"); 
} 
+2

你需要提供更多關於你想要做什麼的更多信息。這是很模糊的。 – Carcigenicate

+4

真的不清楚你在問什麼,但請注意,你已經提供了很多代碼,並且沒有解釋,也沒有試圖將它縮小到[mcve]的跡象。我會說'如果(board = true)'看起來明顯不禮貌 - 你是否知道'='是賦值操作符,而'=='是用於相等性測試的? –

+0

當我運行它時,真實部分起作用,但不起作用。此外,您董事會部分工作正常。 –

回答

3

您的if聲明不會如您所期望的那樣工作。讓我們舉個例子來看看爲什麼:

if(board = true & board != false) 

這有許多的問題:

  1. 你並不需要明確地比較truefalse。這樣做被認爲是不好的風格。 if (board)if (board == true)完全相同。
  2. board = true轉讓,沒有比較。你在幾個地方確實有這個錯誤。這是您的if報表無法正常工作的主要原因之一:if (board = true)不可能爲假,因此else語句無法運行。仔細檢查您的代碼是否存在其他您感到困惑的情況===(有幾個)。
  3. if (board == true && board != false)是多餘的,可能會誤解boolean值的工作方式。如果(board == true),則當然board != false。想想看,如果board == true && board == false;在什麼情況下可以這樣可能是真的?請注意,根據定義,boolean的值爲或者true錯誤(但不是兩者,當然也不是第三個值);只有兩種可能的選擇。

我一定會鼓勵您回顧一下Java中的比較工作方式,以及this rather excellent article on debugging。您特別想關注如何設置斷點以及在哪裏設置斷點(即IDE將暫停執行並允許您檢查程序狀態的點)以及如何遍歷代碼(即,一次只運行一行,讓您可以看到每行代碼的效果)與調試器。

+0

謝謝你會嘗試這個 –

+0

非常感謝。我做了你的建議,現在它工作得很好,我現在會在將來考慮=和==之間的區別。 –

2

歡迎使用編程!

在Java中,使用=符號或運算符進行賦值,這意味着任何在=右側的內容都將保存到左側的任何內容。

你想要的標誌是==。該==標誌是一個比較標誌,這主要是用來布爾根據您的意見

(其中持有任一truefalse變量),我會說你應該簡單地避免裝箱隨後的部分,現在想起這對於登上他們自己的代碼塊。這樣,如果玩家決定登船,你將只執行逃離檢查。

例如:

// Prompt user to see if they want to board, capture input. 
    if (board) { 
    adventurePostBoarding(); 
    } 

,並定義方法如下這樣:

private final void adventurePostBoarding() { 
    // TODO code here 
} 

在未來,記得要比較使用==,而不是=truefalse值。

+2

這個答案可以通過解釋這個問題的原因得到很大的改善,特別是因爲OP是編程新手。例如,它*在技術上解決了主要問題(OP混淆了'='和'=='的事實),但這並不能解釋這是問題所在。 – EJoshuaS

+0

感謝您的建議!我會編輯我的答案以包含該內容。 –

相關問題