如何正确输出我的方法体?

时间:2015-11-05 08:49:29

标签: java methods

我是Java新手,我正在编写一个正在编写程序的麻烦。在TreatHouse.java中,我使用了一个名为passOutCandy的方法,当糖果传递给捣蛋鬼时,它会导致CandyPot1,CandyPot2和totalCandy的值减少。该程序正在运行,但它一直停留在passOutCandy上,并且它不会像它应该减去任何糖果。有人可以解释为什么会这样吗?此外,我们非常感谢任何其他改进我的代码的评论。谢谢!

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("\nHow 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) 
       {
          //Constructor
           if (totalCandy<=0)
           {
              System.out.println("We can't give out candy if we dont have any. I think we have some from last year."
                               + "Yep, we have 100 pieces of candy to give out.");
              totalCandy=100;
           }    
          switch(candyPot)
          {
          case 1:
             candyPot=1;
             break;
          case 2:
             candyPot=2;
             break;
          default:
              System.out.println("Invalid input; There is no pot " + candyPot + ", so we will use candy pot 1 first.");
             candyPot=1;
          }                   
           currentPot=candyPot;                    
          //Add if statement to split the candy evenly between the two pots.       
          if (totalCandy>0){ 
             totalCandy=totalCandy;
            candyPot1= (totalCandy/2);
             candyPot2= (totalCandy/2);

          }else if ((totalCandy%2!=0)&&(totalCandy>0))
              candyPot1=(totalCandy/2);
              candyPot2=(totalCandy-(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 (currentPot==1){
             if (candyPot1>(treatsPerTreater*currentTreaters))
                 candyPot1-=(treatsPerTreater*this.currentTreaters);
                 //switch to pot 2
                 currentPot=2;
          }else if (currentPot==2){
             if (candyPot2>(treatsPerTreater*currentTreaters))
                 candyPot2-= (treatsPerTreater*this.currentTreaters);
                 //switch to pot 1
                 currentPot=1;         
          }else
             System.out.println("Mwhahaha...you've been tricked! No candy for you!!!");        
        }

        //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() 
       {
          System.out.println("Candy left in pot 1: " + candyPot1);
          System.out.println("Candy left in pot 2: " + candyPot2);
          System.out.println("Total candy left: " + (candyPot1 + candyPot2));
        }
    //returns the pot number for which candy was last given.
    public int getLastPot() 
   {
   if (currentPot==1){
      currentPot=1;
      return currentPot;

   }else if(currentPot==2)
      currentPot=2;   
        return currentPot;
   }
    public void setTreatsPerTreater(int treatsPerTreater)
   {
        treatsPerTreater=treatsPerTreater;
    }
}

1 个答案:

答案 0 :(得分:1)

问题是你的setTreatsPerTreater,正如Kayaman所说。属性的值永远不会更新,默认为0。所以,这一行:

candyPot1-=(treatsPerTreater*this.currentTreaters);

等于:

candyPot1-= 0;

你必须使用this.treatsPerTreater。