如果声明没有正常工作

时间:2015-11-05 00:13:20

标签: java if-statement

我是java的新手并且我的代码有问题,程序编译得很好,但是我在TreatHouse.java中遇到了if语句的问题,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;
    }
}

2 个答案:

答案 0 :(得分:1)

您使用变量名totalCandy作为本地方法参数名称和字段名称。

更改此行

totalCandy = totalCandy;

this.totalCandy = totalCandy;

答案 1 :(得分:0)

我无法完全遵循你的逻辑,但这就是我得到的: 你有totalCandy作为Field和参数。如果使用with(1,-3)调用构造函数,则为字段(类变量)分配100.在下一个if语句中,使用了哪个totalCandy?我不记得java如何处理它,但你可能想使用this.totalCandy来引用类变量和没有这个的参数以避免混淆。 在getCandyCount()中你有candyPot1 + candyPot2,但是这些值在if语句中设置为totalCandy / 2.如果totalCandy是奇数,你将计算将减1,因为整数除法会截断余数。 最后,你的if(true)完全没必要。至于你遇到问题的if语句,我会确保先处理其他的东西,然后检查它是否仍然存在。