为什么该方法不生成 return 一个 int?

Why doesn't the method breed return an int?

当调用 breed 方法并且有可用食物时,蜂群的大小会翻倍。询问用户他们想要喂养群体多少次以及他们想要群体繁殖多少次。我需要输出它成功繁殖的次数。我创建了一个名为 success 的整数来跟踪它成功繁殖的时间,但是(对我来说不足为奇)不起作用并且 returns 0。我该如何解决这个问题?谢谢

AmoebaColony 测试器 class

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


}

 }

变形虫群落

 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 breed times: 7 // 在这种情况下它将成功繁殖 4 次 isVitamin:没有维生素(错误)

我会推荐 运行 通过一些简单的调试。按照这些步骤操作,它应该会缩小您的问题所在。

  1. 打印.breed() 的返回值。如果它不是您所期望的,那么您的函数没有正确返回。
  2. 检查您的 breed() 函数的值是否设置正确。例如。它们实际上是您在每一步输入的值。我会添加打印语句以查看是否是这种情况。
  3. 将其与输出值进行比较。如果它们不匹配(例如,您的成功在计算时是正确的,但在最后打印时却不是),然后寻找可能在过程中间进行更改的内容。

从我所看到的代码来看,我没有看到它不应该正确更新成功的原因。

编辑 我明白你的问题了。您正在输入指定的值,这就是程序解释它的方式。您的 breed 函数使用以下值,您输入 breed=7, feed=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;
 }