简体   繁体   English

为什么我的 for 循环只返回我期望的一半元素?

[英]Why does my for loop return only half the elements I expect it to?

I would like to receive user input on n elements for the Fibonacci sequence.我想接收关于斐波那契数列的 n 个元素的用户输入。 The scanner and calculations themselves seem to be working fine -- the input is successfully grabbed and the sequence calculated.扫描仪和计算本身似乎工作正常——输入被成功抓取并计算出序列。 It's even able to correctly disqualify inputs of a negative value.它甚至能够正确取消负值输入的资格。 However, it is not actually returning 'n' elements.但是,它实际上并没有返回“n”个元素。 Curiously, I noticed that the loop consistently returns exactly half the elements requested, albeit in the correct order.奇怪的是,我注意到循环始终返回所请求元素的一半,尽管顺序正确。 If n = 20, the first 10 numbers are returned.如果 n = 20,则返回前 10 个数字。

Here's the code:这是代码:

public class fibonacci_input {

    public static void main(String[] args) {
// TODO Auto-generated method stub

        Scanner myObj = new Scanner(System.in);
        int num1 = 0;
        int num2 = 1;
        int n;
            System.out.println("How many elements would you like in your Fibonacci sequence?");
            n = myObj.nextInt();
                if (n < 0)
                    System.out.println("Positive numbers only!");
                else 
                    System.out.println("Your Fibonacci sequence with " + n + " elements is:");
                    { for (int i = 1; i <= n; ++i)
                        { 
                            System.out.print(num1 + " ");
                            int num3 = num1 + num2;
                            num1 = num2;
                            num2 = num3;
                            ++i;
                        }

                    }
            }
}

I'm struggling to imagine what the problem could be.我正在努力想象问题可能是什么。 I first thought it might be a problem with the test condition statement, but similar (working) solutions online seem to have their loops formatted almost identically.我首先认为这可能是测试条件语句的问题,但是在线类似(工作)解决方案似乎具有几乎相同的循环格式。 Is it perhaps a problem with my variable declarations?我的变量声明可能有问题吗? I've tried multiple changes to no avail so would appreciate another set of eyes:)我尝试了多次更改都无济于事,所以会欣赏另一双眼睛:)

You could just delete the second i++;您可以删除第二个i++; statement in your code, this one is making the for loop run half times as expected, since the i variable is being incremented twice on each iteration代码中的语句,这个语句使 for 循环按预期运行一半,因为i变量在每次迭代中递增两次

public class fibonacci_input {

    public static void main(String[] args) {

        Scanner myObj = new Scanner(System.in);
        int num1 = 0;
        int num2 = 1;
        int n;
            System.out.println("How many elements would you like in your Fibonacci sequence?");
            n = myObj.nextInt();
                if (n < 0)
                    System.out.println("Positive numbers only!");
                else 
                    System.out.println("Your Fibonacci sequence with " + n + " elements is:");
                    { for (int i = 1; i <= n; ++i)
                        { 
                            System.out.print(num1 + " ");
                            int num3 = num1 + num2;
                            num1 = num2;
                            num2 = num3;
                            //++i;
                        }

                    }
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM