Java: 如何让 while 循环只输出最后一行

Java: How to get while loop to only output last line

刚开始学习 java 所以这让我卡住了很长一段时间。这是一个输出斐波那契数列数字的程序。这里的目标是让程序只输出 while 循环的最后一行。我已经启动了程序 运行,只是无法弄清楚如何只输出序列的最后一行

示例Input/Output 输入:5

输出: 斐波那契 #3 是:3 斐波那契 #4 是:5 斐波那契 #5 是:8

输出应如下所示: 斐波那契 #5 是:8

import javax.swing.JOptionPane;

public class Fibonacci2 {

    public static void main(String args[ ]) {

       int n, 
           counter = 2, 
           sum,    
           prevN, 
           nextN; 

       String inputValue;

       inputValue = JOptionPane.showInputDialog(null, "Enter Number Greater than 2: ",
                              JOptionPane.QUESTION_MESSAGE);
       n = Integer.parseInt(inputValue);

       if (n >= 2)

       counter = 2; 
       prevN = 1;
       nextN = 2;

       if (n < 2) 
       System.out.println("Invalid Input. Please Try Again.");


       while (counter < n)
       {
           sum = prevN + nextN;
           prevN = nextN;
           nextN = sum; 
           counter++;

           System.out.println("Fibonacci #"+counter+" is: " + sum); 

       }

       System.exit(0);

    } 

} 

放在循环外就可以了

while (counter < n){
sum = prevN + nextN;
prevN = nextN;
nextN = sum; 
counter++; 
}
System.out.println("Fibonacci #"+counter+" is: " + sum);
}

您唯一需要做的就是将 System.out.println("Fibonacci #"+counter+" is: " + sum) 带出循环,这样它只会打印最后一个数字; 像这样:

while (counter < n)
   {
   sum = prevN + nextN;
   prevN = nextN;
   nextN = sum; 
   counter++;
   }
System.out.println("Fibonacci #"+counter+" is: " + sum);

您问题的简单答案是,将 System.out.println("Fibonacci #" + counter + " is: " + sum); 语句移出 while-loop 上下文...

while (counter < n) {
    sum = prevN + nextN;
    prevN = nextN;
    nextN = sum;
    counter++;
}

System.out.println("Fibonacci #" + counter + " is: " + sum);

I've tried that, it gives me this: Fibonacci2.java:62: error: variable sum might not have been initialized System.out.println("Fibonacci #"+counter+" is: " + sum); ^

与实例字段不同,局部变量在创建时不会被赋予默认值,所以如果代码的路径不能保证它会被设置,你需要确保该值具有默认值...

int n,
        counter = 2,
        sum = 0,
        prevN,
        nextN;

此外,您的 if 陈述令人担忧...

if (n >= 2)

counter = 2; 
prevN = 1;
nextN = 2;

if (n < 2) 
System.out.println("Invalid Input. Please Try Again.");

基本上这是在说什么...

  • 如果 n 大于或等于 2,则 counter 等于 2
  • prevN 等于 1
  • nextN 等于 2
  • 如果 n 小于 2 则输出错误信息
  • 继续运行循环

对我来说,这没有多大意义,也很难阅读。相反,您应该包含应在其自己的执行上下文中(即 {...} 大括号之间)为每个分支执行的代码,例如...

if (n >= 2) {
    counter = 2;
    prevN = 1;
    nextN = 2;

    while (counter < n) {
        sum = prevN + nextN;
        prevN = nextN;
        nextN = sum;
        counter++;
    }

    System.out.println("Fibonacci #" + counter + " is: " + sum);
} else {
    System.out.println("Invalid Input. Please Try Again.");
}

这使代码更易于阅读和理解 - 但我头脑简单

我为我的 EGR 制作了一个类似的程序 class;它的目标是输出一个给定位置的数字(序列从 1 开始),即 p1=1、p2=1、p3=2、p4=3、p5=5、p6=8 等

import java.util.Scanner;

public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);

System.out.println("Enter the Fibonacci Number's position.");
long n=keyboard.nextLong();
long sum, prevN=1, nextN=0, counter=1, last=0, lastnum;


    while (counter <= n) {
        sum = prevN + nextN;
        prevN = nextN;
        nextN = sum;
        if (n == counter)
        {
             System.out.println("Fibonacci #" + counter + " is: " + sum);
        }
        counter++;      
    }
    
    

}}

我知道对 OP 来说已经晚了,但这是我对任何其他 students/enthusiasts 寻求帮助的人的两分钱。