2015-11-05 83 views
0

我是新來的java和有我的代碼的問題,該程序compliles就好了,但我有一個與TreatHouse.java,如果語句的問題,if ((totalCandy > 0) && (treatsPerTreater < totalCandy)) { 不給我正確的輸出。我試圖弄清楚如何解決這個問題,以及如何改進我目前的代碼。任何幫助將非常感激。如果陳述不正確

import java.util.Scanner; 

public class Halloween { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 

     System.out.println("Which candy should we give out first? Candy in pot 1 or candy in pot 2?"); 
     int candyPot = scan.nextInt(); 

     System.out.println("How much candy did we buy?"); 
     int totalCandy = scan.nextInt(); 

     TreatHouse ourHouse = new TreatHouse(candyPot, totalCandy); 

     while (ourHouse.getCandyCount() > 0) { 
      ourHouse.getCandyStatus(); //tells how much candy is left & other stuff 

      System.out.println("How much candy per treater should we give out?"); 
      int treatsPerTreater = scan.nextInt(); 
      ourHouse.setTreatsPerTreater(treatsPerTreater); 

      System.out.println("Knock, knock...." + "Trick or treat!"); 
      ourHouse.knockKnock(); 
      ourHouse.passOutCandy(); 
     } 

     System.out.println("Time to turn off the lights and go to bed!"); 
     System.out.println("The last candy came from pot number"+ ourHouse.getLastPot()); 
     System.out.println("Happy Halloween!"); 
     scan.close(); 
    } 
} 












import java.util.Random; 

public class TreatHouse { 
    int candyPot1; //amount of candy in pot 1 
    int candyPot2; //amount of candy in pot 2 
    int currentPot; //1 or 2 
    int totalCandy; 
    int currentTreaters; 
    int treatsPerTreater; 

    public TreatHouse(int candyPot, int totalCandy) { 
     //Add code here, be sure to split the candy between the pots. 
     currentPot = candyPot; 
     if (totalCandy <=0) { 
      System.out.println("We can't give out candy if we don't have amy. I think we have some from last year. " + 
          "Yep, we have 100 pieces of candy to give out."); 
      totalCandy = 100; 
      candyPot1 = totalCandy/2; 
      candyPot2 = totalCandy/2; 
     } 

     if (totalCandy > 0) { 
      totalCandy = totalCandy; 
      candyPot1 = totalCandy/2; 
      candyPot2 = totalCandy/2; 
     }        
    } 

    public int getCandyCount() { 
     return candyPot1 + candyPot2; 
    } 

    public void passOutCandy() { 
     //If there are enough treats per treater for the given amount per treater, pass out candy 
     //from the current pot and switch to the other one. 
     //Else display a message that the treaters have been tricked... (no candy for them.) 
     // but do not change the current pot 
     if ((totalCandy > 0) && (treatsPerTreater < totalCandy)) { 
      if (true) { 
      if (currentPot == 1) { 
       candyPot1 = candyPot1 - (this.currentTreaters*treatsPerTreater); 
       currentPot = 2; 
      } 

      if (currentPot == 2) { 
       candyPot2 = candyPot2 - (this.currentTreaters*treatsPerTreater); 
       currentPot = 1; 
      } 

      // System.out.println("Pot " + currentPot + " doesn't exist. Try again next time"); 
      } 
     } 
     else 
      System.out.println("You have been tricked! No candy for you >:D!!!!");    
    } 


    //Sets the number of trick or treaters. 
    public void knockKnock() { 
     Random gen = new Random(System.currentTimeMillis()); 
     this.currentTreaters = gen.nextInt(13) + 1; //1 to 13 treaters. 
    } 

    //Displays how much candy in each pot, total candy left 

    public void getCandyStatus() { 
     //add in code here 
     System.out.println("Candy in Pot1: " + candyPot1 + " Candy in Pot2: " + candyPot2); 
     System.out.println("Total candy left: " + (candyPot1 + candyPot2)); 

    } 

    //returns the pot number for which candy was last given. 
    public int getLastPot() { 
     //add code here 
     if (currentPot == 1 || currentPot == 2) { 
      if (currentPot == 2) { 
       currentPot = 1; 
       return currentPot; 
      } 
      if (currentPot == 1) { 
       currentPot = 2; 
       return currentPot; 
      } 
     } 
     else 
      return currentPot; 

     return 1; 
    } 

    public void setTreatsPerTreater(int treatsPerTreater) { 
     //add code here 
     treatsPerTreater = treatsPerTreater; 
    } 
} 
+0

「問題尋求幫助調試必須包括***所需的行爲***,***一個特定問題***或錯誤在問題本身中重現它所需的最短代碼。沒有明確問題陳述的問題***對其他讀者無益。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。「 - 理想情況下,您的問題應該包含_expected_行爲和_current_行爲。 「不給我正確的輸出」作爲問題描述是無用的。 – Xufox

回答

0

我不能完全按照你的邏輯,但這裏是我的了: 你有totalCandy作爲一個字段和參數。如果你用(1,-3)調用構造函數,那麼字段(類變量)被賦值爲100.在下一個if語句中,使用了totalCandy中的哪一個?我不記得java是如何處理這個的,但是你可能想要使用this.totalCandy來引用類變量和參數,而不是爲了避免混淆。 在getCandyCount()中,您有candyPot1 + candyPot2,但是這些值在if語句中設置爲totalCandy/2.如果totalCandy爲奇數,那麼您將計數將由於整數除法而截斷餘數。最後,你的if(true)是完全不必要的。至於你有問題的if語句,我會確保首先處理其他的東西,然後檢查它是否仍然存在。

+0

'if(true)'很可能暫時用於測試前面的if語句。 – Xufox

+0

不,字段totalCandy從不分配,他在兩種情況下重新分配本地參數。 –

+0

@Avezou我將如何修復整數截斷?其邏輯背後的邏輯是,如果我輸入41(任何奇數)糖果並且需要正確分割,那麼我將得到20個這兩個底池,我如何編碼以獲得20和21的合適底池? – YellowBird

1

對於本地方法參數名稱以及字段名稱,都使用變量名稱totalCandy

改變這一行

totalCandy = totalCandy; 

this.totalCandy = totalCandy;