无法减少总数

时间:2015-11-05 16:43:02

标签: java if-statement

我正在研究这个非常具有挑战性的计划,我很难理解。我已经相当远了它但是我很难在每次循环后减少糖量。如何让每罐糖果随总量减少?谢谢你的帮助!

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

   }
}

3 个答案:

答案 0 :(得分:0)

提示 - 摆脱this.totalCandy - 在构造函数完成后你不需要它,所有糖果都被分成了花盆。

将总数保存在一个单独的变量中是不必要的,因为你可以从每个底池的糖果量计算它 - 实际上有两种方式表示相同的数字(总糖果)(如totalCandy和总和所有花盆中的糖果)使得程序更难以正确编写并且更难维护;在你的情况下,它确实是问题的原因。此建议也称为Don't Repeat Yourself principle

答案 1 :(得分:0)

我怀疑问题在这里

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

这里你没有使用传递的参数。 currentTreaters为0,这也会导致treatsPerTreater为0。所以当你打电话给ourHouse.passOutCandy();时,'1号和2号的值不会改变。

答案 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