Fibonacci循环不断询问

时间:2015-10-05 14:11:22

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

这是我的代码,它告诉你增加你要求的斐波纳契数字,并在你插入" -1"时停止,一切正常。我需要它一直告诉我相同的数字但是如果我插入的话恩。 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);
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

    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;
    ....
    }