Java 代码似乎跳过了if或for循环

Java 代码似乎跳过了if或for循环,java,Java,当我输入满足所有条件且不会触发任何错误的输入时,程序会在最后一次输入后退出,就像跳过for或if循环一样 同样在System.out.printf之后(“输入第二个物种的名称:”)它不允许任何输入,只是跳到下一个提示。我不知道这是为什么。上面询问第一个物种信息的部分很好 这是因为,在物种2超过物种1之后,你永远不会打印任何东西,除非在非常特殊的情况下,物种2和物种1在某一年中拥有完全相同的种群 这是因为,当您输入物种1的生长率时,您输入一个整数,然后按enter键keyboard.nextLi

当我输入满足所有条件且不会触发任何错误的输入时,程序会在最后一次输入后退出,就像跳过
for
if
循环一样

  • 同样在
    System.out.printf之后(“输入第二个物种的名称:”)它不允许任何输入,只是跳到下一个提示。我不知道这是为什么。上面询问第一个物种信息的部分很好

  • 这是因为,在物种2超过物种1之后,你永远不会打印任何东西,除非在非常特殊的情况下,物种2和物种1在某一年中拥有完全相同的种群

  • 这是因为,当您输入物种1的生长率时,您输入一个整数,然后按enter键
    keyboard.nextLine()
    接受整数,但在输入缓冲区中保留换行符,因此后续的
    keyboard.nextLine()
    认为有一个空行在等待它

  • 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");
                    }
                }
            }
        }
    }
    
    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;
        }