2015-09-25 102 views
0

所以你好我得到無限循環問題我不知道我的代碼是什麼錯我試圖使數字序列格式是在底部我認爲問題是在我的情況?Java數字序列循環

import java.util.Scanner; 
public class tester { 
    public static void main(String[] args) { 
     Scanner x = new Scanner(System.in); 


     int n;   

     System.out.print("Enter how many numbers to display"); 
     n = x.nextInt(); 


     while(n!=0) {      //is this right? 
      for (int i = 0; i<=n; i++) { 
       if(i%2==0) { 
        n += 2; 
        System.out.print(n); 

       } else { 
        n += 3; 
        System.out.print(n); 
       } 
      } 

     } 
    } 
} 

輸出我想要得到

Enter how many numbers to display : 5 
1 3 6 8 11 

2. 
Enter how many numbers to display : 16 
1 3 6 8 11 13 16 18 21 23 26 28 31 33 36 38 //but im getting infinite loops 

// the sequence pattern is +2 then +3 
+1

不需要'while'。 – Satya

回答

3

的問題是在這裏:while(n!=0)這裏:for (int i = 0; i<=n; i++)。對於while循環,將繼續前進,直到n等於0.對於for循環,這很可能會持續進行。

您的代碼有兩個問題:

  1. 如果你提供一個非負值,這會一直下去,直到永遠(因爲你永遠只增加n)。
  2. 即使你確實提供了一個負數,n也需要完全停止。

根據您需要做什麼,您將需要更改條件。從輸出結果來看,n需要是正數,因此您需要規定n的一些上限範圍,其中while循環將停止。

編輯:你只需要有1循環做你以後的事情。另外,n表示元素的數量,因此在整個程序執行過程中需要保持固定。在你的情況下,你一直在增加它。

Scanner x = new Scanner(System.in); 
    int n; 
    System.out.print("Enter how many numbers to display"); 
    n = x.nextInt(); 

    int count = 0; 
    int i = 1; 
    while (count < n) {      //is this right?    
     if (count % 2 == 0) { 
      System.out.print(i + " "); 
      i += 2; 
     } else { 
      System.out.print(i + " "); 
      i += 3;     
     }    
     count++; 
    } 
+0

好吧,我應該把最高38?這意味着16? – Jake

+0

@Jake:請參閱我的編輯。 – npinti

1

使用 '如果' 條件的地方 '而' 循環

+0

仍然給出了一個無限循環,我認爲它在if語句中的狀態? – Jake

+0

雅賴特,你正在改變循環中的'n'值,這也是無限循環的原因。 'for'循環中的' – KishoreReddy

+0

','i'遞增1步,同時'n'遞增2或3步。最好使用另一個局部變量,而不是改變'n'值。 – KishoreReddy

2

兩個問題:

int stop = n; // declare one local var to stop the for loop 

if (n != 0) { //switch to if condition 
    for (int i = 0; i <= stop; i++) { 
     //loop's exit condition wasn't met because 'n' was also being incremented 
     if (i % 2 == 0) { 
      n += 2; 
      System.out.print(n+" "); 

     } else { 
      n += 3; 
      System.out.print(n+" "); 
     } 
    }  
} 
1

你必須像這樣有if-condition取代你while-loop

import java.util.Scanner; 
public class tester { 
    public static void main(String[] args) { 

     Scanner x = new Scanner(System.in);   
     int n;   

     System.out.print("Enter how many numbers to display"); 
     n = x.nextInt(); 
     int stop = n; 

     if(n!=0) { //if statement checks if n!=0 
      for (int i = 0; i<=stop; i++) { 
        //stop replaces n because n is incremented in your for-loop 
       if(i%2==0) { 
        n += 2; 
        System.out.print(n); 

       } else { 
        n += 3; 
        System.out.print(n); 
       } 
      } 
     } 

    } 
} 
+0

仍然給出了無限循環 – Jake

+0

你是否也改變了for循環?它不應該循環無限 –

0

根據你的回答,我發現了一個可行的解決方案:

int n;  

System.out.print("Enter how many numbers to display"); 
n = x.nextInt(); 
int k = -2; // so that it starts with 1 when i add +3 
int stop = n-1; 

if(n!=0) {      
    for (int i = 0; i<=stop; i++) { 
     if(i%2==0) { 
      k += 3; 
      System.out.print(k+" "); 
     } else { 
      k += 2; 
      System.out.print(k+" "); 
     } 
    } 
} 
+0

雖然發佈您的最終解決方案作爲答案是有幫助的,但一定要將其他答案標記爲已接受,無論哪個答案最能幫助您獲得解決方案。 – CubeJockey

+0

好,然後把你upvotes:D我的answere應該已經擺脫了infinit循環... –