为什么方法品种没有返回int?

时间:2015-09-22 16:26:40

标签: java random joptionpane

当调用品种方法并且有食物供应时,殖民地的大小增加一倍。用户被问及他们想要给殖民地喂食多少次以及他们希望殖民地繁殖多少次。我需要输出它成功繁殖的次数。我创建了一个名为success的整数来跟踪它成功繁殖的次数,但(对我来说并不奇怪)不起作用并返回0.我如何解决这个问题?感谢。

AmoebaColony测试员班

import javax.swing.JOptionPane;


public class AmoebaColonyTester {

    public static void main(String[] args) {

    String name = JOptionPane.showInputDialog(null, "What will be the name of the colony?");
    String caretakerName = JOptionPane.showInputDialog(null, "What is the name of the caretaker of the colony?");
    int colonySize = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the starting size of the colony?"));
    int feedTimes = Integer.parseInt(JOptionPane.showInputDialog(null, "How many times do you want to feed the colony?"));
    int breedTimes = Integer.parseInt(JOptionPane.showInputDialog(null, "How many times do you want your colony to breed?"));
    int vitamin = JOptionPane.showConfirmDialog(null,"Do you want to give vitamins to your colony?", "Please select",
            JOptionPane.YES_NO_OPTION);
    boolean isVitamin;
        if (vitamin == 1)
            isVitamin = true;
        else
            isVitamin = false; 
    int success = 0;

    AmoebaColony amoeba = new AmoebaColony(name, caretakerName, colonySize, feedTimes,
                                           breedTimes, isVitamin);

    for(int x = 1; x <= breedTimes; x++)
    {
        success += amoeba.breed(); 
    }

    amoeba.howManyDead();

    System.out.println("the size of the colony is " + amoeba.getSize());

    JOptionPane.showMessageDialog(null, "The colony name is " + name + "\nThe caretaker name is " + caretakerName +
                                 "\nThe starting colony size was " + colonySize + "\nThey were fed " + feedTimes
                                 + " times \nRequested number of times to breed: " + breedTimes
                                 + "\nThey successfully bred " + success + "times \nThe final size of the"
                                 + " colony is " + amoeba.getSize());


}

 }

AmoebaColony

 public class AmoebaColony {

private String name;
private String caretakerName;
private int colonySize;
private int feedTimes;
private int breedTimes;
boolean isVitamin; 
int successfulBreeds;

public AmoebaColony(String name, String caretakerName, int colonySize,
        int feedTimes, int breedTimes, boolean isVitamin) {
    this.name = name;
    this.caretakerName = caretakerName;
    this.colonySize = colonySize;
    this.feedTimes = feedTimes;
    this.breedTimes = breedTimes;
    this.isVitamin = isVitamin;
}

public int getSize()
{
    return colonySize;
}

public int breed()
{
  if(breedTimes <= feedTimes){
    colonySize *= 2;
    feedTimes--;
    breedTimes--; 
    return 1;
 }
  else{
    feedTimes--;
    breedTimes--;
    return 0;
 }

}

  public int howManyDead()
  {
      Random random = new Random();
      int pop = colonySize; 

      if (isVitamin)
      { 
          int c1 = random.nextInt(4);
          if (c1 == 1)
          colonySize = colonySize - (colonySize/10);
          else 
          colonySize = colonySize;

      }
      else if (isVitamin == false)
      {
          int c2 = 1 + random.nextInt(3);
          if (c2 == 1)
              colonySize = colonySize - (colonySize/10);
          else 
              colonySize = colonySize;
      }

      return pop - colonySize;

  }


 }

示例输入: 殖民地名称:bobo 看守者的名字:彼得 菌落大小:500 饲料时间:4 繁殖时间:7 //在这种情况下它将成功繁殖4次 是维生素:没有维生素(假)

1 个答案:

答案 0 :(得分:0)

我建议通过一些简单的调试来运行它。按照以下步骤操作,它应该缩小您的问题范围。

  1. 打印.breed()的返回值。如果它的其他任何东西,那么你的期望,你的功能没有正确返回。
  2. 检查您的breed()函数的值是否设置正确。例如。它们实际上是您在每一步输入的值。我会添加print语句以查看是否是这种情况。
  3. 将此值与输出值进行比较。如果它们不匹配(例如,在计算时你的成功是正确的,而不是在最后打印它时),那么寻找可能在过程中间进行更改的东西。
  4. 从我所看到的代码中,我没有看到它不能正确更新成功的原因。

    修改我看到了您的问题。您正在输入指定的值,这是程序解释它的方式。您的品种功能使用以下值,您输入品种= 7,饲料= 4。我把这些值放到下面的函数中......

    if(7<= 4){ // this is obviously false, so it uses the else
        colonySize *= 2;
        feedTimes--;
        breedTimes--; 
        return 1;
     } else{
        //Since this is run, and both are decremented togeather, you will always be in the Else.
        feedTimes--;
        breedTimes--;
        return 0;
     }