【问题标题】:Fibonacci loop that keeps on asking不断询问的斐波那契循环
【发布时间】:2016-01-02 05:36:33
【问题描述】:

这是我的代码,它告诉您询问的递增斐波那契数,并在您插入“-1”时停止,一切正常。我需要它一直告诉我相同的数字,但如果我插入 ex。 12 它说:89,这是正确的。但是我第二次这样做时,它给了我 10946 等等,总是增加。想法如何解决这个问题并让 89 或其他数字保持不变?

int first= 0;
int second= 1;
int next= 0;
Scanner scanner  = new Scanner(System.in);
System.out.println("tell the fibonacci number you want to know:");
 while(true){
int fib= scanner.nextInt();// 0, 1, 1, 2, 3, 5, 8, 13, 21

if(fib== -1){  System.out.println("program ends");break;}
else if(fib== 2)System.out.println("1");
else{ 
  if(fib==0){System.out.println("error, tell fibonacci one more time: "); 
    continue; //This will jump to the next loop of while, asking the number fib.  Not really necessary with the if else construct done here.
}
else{        
    for(int count= 3; count<=fib;count++){
    next= first+second;            
    first= second;
    second=next;}            
System.out.println(next);
}

【问题讨论】:

  • 你认为while (true) 是做什么的?你认为这个循环会在什么情况下退出?
  • 在相关说明中,您说在尝试实现循环逻辑时“出现错误/意外循环”。那可能是什么错误?

标签: java loops for-loop while-loop fibonacci


【解决方案1】:
public static void main(String[] args) {
    int first= 0;
    int second= 1;
    int next= 0;
    Scanner scanner  = new Scanner(System.in);
    System.out.println("tell the fibonacci number you want to know:");
     while(true)   //<<--- Here. You will execute the next line forever.
    int fib= scanner.nextInt();// 0, 1, 1, 2, 3, 5, 8, 13, 21

    if(fib== -1){  System.out.println("program ends");}
    else if(fib== 2)System.out.println("1");     
    else { 
        while(fib==0){
            System.out.println("error, tell fibonacci one more time: "); 
            fib = scanner.nextInt();
        }        
        for(int count= 3; count<=fib;count++){
            next= first+second;            
            first= second;
            second=next;
        }            
    System.out.println(next);        
}    

while(true) 的正确使用方式:

public static void main(String[] args) {
    int first= 0;
    int second= 1;
    int next= 0;
    Scanner scanner  = new Scanner(System.in);
    System.out.println("tell the fibonacci number you want to know:");
    while(true){
        int fib= scanner.nextInt();// 0, 1, 1, 2, 3, 5, 8, 13, 21

        if(fib== -1){ System.out.println("program ends");}
        else if(fib== 2) System.out.println("1");
        else { 
            if(fib==0){
                System.out.println("error, tell fibonacci one more time: "); 
                continue; //This will jump to the next loop of while, asking the number fib.  Not really necessary with the if else construct done here. Also, you can use a switch-case to check the value of fib.
            } else {        
                for(int count= 3; count<=fib;count++){
                    next= first+second;            
                    first= second;
                    second=next;
                 }            
                 System.out.println(next);
            }
      }        
}   

【讨论】:

  • 当我想要例如 fib 数字 12 时,它说 89 是正确的。但是当我第二次问它时,它说 10946。我怎样才能一直得到 89?
  • 简单,将第一个和第二个变量设置为while循环内部的初始值。当这种事情发生时,你应该在脑海中运行一个循环,看看变量会发生什么。如果你检查 for 循环,下一个值会被覆盖而不读取它,但 first 和 second 具有之前 while 循环的值。
【解决方案2】:

将您的扫描仪放入循环中。

    while(true){
     Scanner scanner  = new Scanner(System.in);
     int fib= scanner.nextInt();// 0, 1, 1, 2, 3, 5, 8, 13, 21
     if(fib == -1) break;
    ....
    }

【讨论】:

  • 每次创建一个新的扫描仪会有什么帮助?
  • @Stan 它只计算一个数字。如果要添加多个数字,则必须使用数组。
猜你喜欢
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-30
  • 2010-11-24
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
相关资源
最近更新 更多