2012-10-25 44 views
0

我不知道爲什麼我的第二個for循環將不執行。它編譯,但是當我運行這個它不起作用〜_〜Java的基本循環

import java.util.Scanner; 

public class ranges 
{ 


public static void main (String[] args) 

    { 

Scanner scan = new Scanner (System.in); 

     int size; 
     int input; 
     int count = 0; 
     int occurence = 0; 
     System.out.print ("Please enter the size of your array: "); 
     size = scan.nextInt(); 
     int[] list = new int[size]; 



     for (int index = 0 ; index < list.length ; index++) 
     { 
      System.out.print ("Please enter an integer between 0 to 50: "); 
      input = scan.nextInt(); 

      if (input >= 0 && input <= 50) 
      { 
       list[index] = input; 
      } 
      else 
      { 
       System.out.println ("Invalid output, please try again"); 
       index--; 
      } 
     } 




     int right = (list.length)-1; 
     int left = 0; 

     for (int counter = left ; counter < list.length ; counter++) 
     { 
      while (right >= left) 
      { 
       if (list[left] == list[right]) 
       { 
        occurence++; 
        right--; 
       } 
      } 
      right = (list.length)-1; 
      System.out.println ("The number " + list[left] + " was added " + occurence + "4 times"); 
     } 

     for (int value : list) 
     { 
      System.out.print (value + " "); 
     } 
     ; 








    } 
} 

我更新的循環來evaulate OCCURENCES

對(INT左= 0;左< list.length;左++)

{

 while (right >= left) 
     { 
      if (list[left] == list[right]) 
      { 
       occurence++; 

      } 
      right--; 
     } 


     System.out.println ("The number " + list[left] + " was added " + occurence + " times"); 
     right = (list.length)-1; 
     occurence = 0; 
    } 

我已經洗乾淨了一下,現在出現次數是相同的輸入

+1

你到底做的事:與HashMap的工作樣本? – smk

+1

「它編譯但是當我運行它時它不起作用〜_〜」 - 那麼會發生什麼?你有例外嗎? –

+0

@Mihai Stancu:作業標籤宣佈已過時! – Philipp

回答

1

你第二for也在努力。問題在於while環路條件,即while (right >= left)。如果list[left] == list[right]不相等,則會進入無限循環,因爲在這種情況下會更改rightleft

我想,你需要如下更改while(移動right--if條件外):

while (right >= left) 
    { 
    if (list[left] == list[right]) 
     { 
     occurence++; 
     } 
     right--; 
    } 

另外兩個問題:

while循環之前重新初始化occurence =0;以便它計算每個數字的出現次數,並從您的System.out.println()中刪除4如下:

for (int counter = left ; counter < list.length ; counter++) 
{ 
    occurence = 0; //< initialize to 0 
    while (right >= left) 
    { 
     if (list[left] == list[right]) 
     { 
     occurence++; 
     } 
      right--; 
    } 
    right = (list.length)-1; 
     //remove 4 after "occurance +" 
    System.out.println ("The number " + list[left] + 
              " was added " + occurence + " times"); 
} 

編輯:

 Map<Integer, Integer> scannedNums = new HashMap<Integer, Integer>(); 
     for (int counter = left ; counter < list.length ; counter++) 
     { 
      if(scannedNums.get(list[counter]) == null){ 
       scannedNums.put(list[counter], 1); 
      }else{ 
       int currentCount = scannedNums.get(list[counter]); 
       scannedNums.put(list[counter], currentCount+1); 
      } 
     } 

     Set<Integer> nums = scannedNums.keySet(); 
     Iterator<Integer> numIter = nums.iterator(); 
     while(numIter.hasNext()){ 
      int number = numIter.next(); 
      System.out.println ("The number " + number + 
        " was added " + scannedNums.get(number) + " times"); 
     } 
+0

謝謝,對不起。但它沒有給出正確的數組出現次數,我的公式中是否有錯? – Aaron

+0

@Aaron:還有兩個問題。請看最新的答案。 –

+0

發生變量已經初始化爲0,我已經刪除了4 – Aaron

0

只需快速瀏覽一下,並建議未來。

此代碼:

while (right >= left) 
    { 
     if (list[left] == list[right]) 
     { 
      occurence++; 
      right--; 
     } 
    } 

表明,如果列表[左] =列表[右],但正確的依然是> =左側,這個循環將永遠不會停止!如果你想要的只是發現了多少次事件的計數,你可能想要將if語句的右側移動。

最後,當你說「循環不起作用」時,這有點幫助不大。也許顯示你的輸出,或者它不工作的方式會更好。

+0

謝謝,現在它工作正常,但看起來像公式是錯誤的。我試圖獲得用戶在數組中輸入的相同數字的多少次出現次數。我有正確的公式嗎? – Aaron

+0

顯示您的測試。調試其他人的代碼時,他們真的很煩惱地告訴你它真的在做什麼,這樣更容易。 – billjamesdev

+0

如果我輸入了10,30,10,20,30 ......我預計它會說「10中的2,30中的2,10中的1,20中的1和30中的1」......根據你有什麼。也許你應該查看一個地圖的想法來存儲這些值以及他們輸入了多少次。 – billjamesdev

0

它給了什麼錯誤?

我看到的一個直接問題是,如果沒有發生,while循環將永遠運行。第一個for循環可能會使索引超出範圍。

對於第一個循環,而不是:

for (int index = 0 ; index < list.length ; index++) 
    { 
     System.out.print ("Please enter an integer between 0 to 50: "); 
     input = scan.nextInt(); 

     if (input >= 0 && input <= 50) 
     { 
      list[index] = input; 
     } 
     else 
     { 
      System.out.println ("Invalid output, please try again"); 
      index--; 
     } 
    } 

我會做:

for (int index = 0 ; index < list.length ; ++index) 
    { 
     while(true) 
     { 
      System.out.print ("Please enter an integer between 0 to 50: "); 
      input = scan.nextInt(); 
      if (input >= 0 && input <= 50) 
      { 
       list[index] = input; 
       break; 
      } 
      System.out.println ("Invalid output, please try again"); 
     } 
    } 

而對於第二個循環:

for (int counter = left ; counter < list.length ; counter++) 
    { 
     occurence = 0; 
     for(int i = counter; i <= right; ++i) 
     { 
      if(list[left] == list[i]) 
       ++occurence; 
     } 
     System.out.println ("The number " + list[left] + " was added " + occurence + " times"); 
    }