2015-11-05 51 views
1

我目前正在研究這個非常具有挑戰性的程序,我很難理解。我已經得到了很多,但是在每次循環之後我都無法獲得糖果數量的減少。我將如何讓每一罐糖果隨總量一起減少?感謝您的幫助!無法讓總數減少

import java.util.Random; 

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

    public TreatHouse(int candyPot, int totalCandy) { 
     // ints variable currentPot by parameter candyPot, prints message 
     if(candyPot !=1 && candyPot !=2) { 
     //candyPot = 1; 
     currentPot = 1; 
     System.out.println("Invalid input, we will use candy pot 1 first.");    
    } 

     //ensures total # of candy is more than zero 
     if(totalCandy <= 0){ 
     this.totalCandy = 0; 
     System.out.println("We can't give out candy if we don't have any. " 
           +"\nI think we have some from last year. Yep, we have 100 pieces " 
            +"\nof candy to give out."); 
     }else 
     this.totalCandy = totalCandy; 

     // splits the candy between the pots        
     this.totalCandy = this.totalCandy + 100;        
     candyPot1 = this.totalCandy/2; 
     candyPot2 = this.totalCandy - candyPot1; 
    } 

     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 

      else display a messagethat the treaters have been tricked (No candy!) 
        but don't change the current pot*/ 

     if(currentPot == 1) { 
      if (treatsPerTreater*currentTreaters <= candyPot1) { 
        candyPot1 = candyPot1 - (treatsPerTreater*currentTreaters); 
      } else { 
        System.out.println("Sorry you've been tricked! No treats for you..."); 
      }  
       currentPot = 2;   
     } 
     else if (currentPot == 2){ 
      if (treatsPerTreater*currentTreaters <= candyPot2) { 
        candyPot2 = candyPot2 - (treatsPerTreater*currentTreaters); 
      } 
      else{ 
        System.out.println("Sorry you've been tricked! No treats for you..."); 
       } 
        currentPot = 1;  
     } 
     } 

     // Sets the # 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() { 
     System.out.println("We have " +this.candyPot1+ " pieces of candy left in pot 1 and " + 
        this.candyPot2 + " pieces of candy left in pot 2."); 
     System.out.println("There's a total of " + (this.totalCandy) + " pieces of candy in the two pots.");      
     }    

     //returns the pot number for which candy was last given 
     public int getLastPot() { 
     return candyPot; 
     } 

     public void setTreatsPerTreater(int treatsPerTreater) { 
     treatsPerTreater = currentTreaters*2; 
     } 
} 

這裏的驅動程序:

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 from pot 1 or 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(); 

     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(); 

    } 
} 
+0

總數是多少?答案:部分的總和。你如何做一筆總和?回答:用+操作符。你什麼時候想要總計?回答:這取決於你,但可能在改變了其中一個部分後 – Aaron

+0

真的,我閱讀你的文章越多,我理解你的問題就越少。你有一個getCandyCount()函數來獲取總數。你說你想讓總數減少,但是你已經減少了passOutCandy()函數中的currentPot,這導致糖果總數減少。 – Aaron

回答

0

提示 - 擺脫this.totalCandy - 你不需要它的構造完成後,所有的糖果被分爲盆。

將總數保存在一個單獨的變量中是沒有必要的,因爲您可以從每個罐子中的糖果量計算它 - 實際上具有以兩種方式表示的相同數量(總糖果)(作爲totalCandy以及作爲所有糖果總數鍋)使得程序難以正確編寫,難以維護;在你的情況下,這確實是問題的原因。此建議也被稱爲Don't Repeat Yourself principle

0

我懷疑問題就在這裏

public void setTreatsPerTreater(int treatsPerTreater) { 
    treatsPerTreater = currentTreaters*2; 
} 

在這裏你沒有使用過帕拉姆。 currentTreaters爲0,這將導致treatsPerTreater也爲0。所以當你打電話給ourHouse.passOutCandy();時,pot 1和2的值不會改變。

0

下,您可能會錯誤(據我可以告訴)是這裏唯一的地方,

public void setTreatsPerTreater(int treatsPerTreater) { 
    treatsPerTreater = currentTreaters*2; 
    } 

在這裏,當你修改treatsPerTreater要更改局部變量treatsPerTreater,而不是類變量。

也許你的意思是說,

public void setTreatsPerTreater(int treatsPerTreater) { 
    this.treatsPerTreater = treatsPerTreater; 
    } 

這就是所謂的shadowing

某些聲明可能會在其範圍的一部分被同一名稱的另一聲明遮蔽,在這種情況下,不能使用簡單的名稱來引用聲明的實體。

詳情請參閱this
也看看Jiri Tousek's answer