代码似乎跳过if或for循环

时间:2012-02-20 00:56:35

标签: java

  1. 当我输入满足所有内容并且不会触发任何错误的输入时,程序会在最后一次输入后退出,就像跳过forif循环一样。< / p>

  2. 同样在System.out.printf("Enter the name of your second species: ");之后它不允许任何输入,它只是跳到下一个提示。我不知道为什么会这样。它上面的部分要求第一个物种的信息正常。

  3. import java.util.Scanner;
    
    public class HW2johnson_pp1 {
    
        public static void main(String args[]) {
    
            Scanner keyboard = new Scanner(System.in);
    
            System.out.printf("Please enter the species with the higher" + 
                              " population first\n");
            System.out.printf("Enter the name of your first species: ");
            String Species1 = keyboard.nextLine();
            System.out.printf("Enter the species' population: ");
            int Pop1 = keyboard.nextInt();
            System.out.printf("Enter the species' growth rate: ");
            int Growth1 = keyboard.nextInt();
    
            System.out.printf("Enter the name of your second species: ");
            String Species2 = keyboard.nextLine();
            System.out.printf("Enter the species' population: ");
            int Pop2 = keyboard.nextInt();
            System.out.printf("Enter the species' growth rate: ");
            int Growth2 = keyboard.nextInt();
    
            if (Pop2 > Pop1) {
                System.out.printf("The first population must be higher. \n");
                System.exit(0);
            }
    
    
            Species input1 = new Species();
            input1.name = Species1;
            input1.population = Pop1;
            input1.growthRate = Growth1;
    
            Species input2 = new Species();
            input2.name = Species2;
            input2.population = Pop2;
            input2.growthRate = Growth2;
    
            if ((input1.predictPopulation(1) - input2.predictPopulation(1)) <= 
                (input1.predictPopulation(2) - input2.predictPopulation(2))){
    
                System.out.printf(Species2 + " will never out-populate " + 
                                  Species1 + "\n");
            }
            else {
    
                for (int i = 0; input2.predictPopulation(i) <= 
                                input1.predictPopulation(i); i++) {
    
                    if (input2.predictPopulation(i) == input1.predictPopulation(i)) {
                        System.out.printf(" will out-populate \n");
                    }
                }
            }
        }
    }
    

    这适用于predictPopulation()

    public int predictPopulation(int years)
        {
            int result = 0;
            double populationAmount = population;
            int count = years;
            while ((count > 0) && (populationAmount > 0))
            {
                populationAmount = (populationAmount +
                              (growthRate / 100) * populationAmount);
                count--;
            }
            if (populationAmount > 0)
                result = (int)populationAmount;
    
            return result;
        }
    

1 个答案:

答案 0 :(得分:1)

  1. 这是因为在物种2超过物种1后你永远不会打印任何东西,除非特殊情况下物种2和物种1在某些年份完全相同的人口。

  2. 这是因为,当您输入Species 1的增长率时,输入一个整数,然后按 Enter keyboard.nextInt()吞下整数,但在输入缓冲区留下换行符,因此后续keyboard.nextLine()认为在那里等待它的空行。