2011-04-27 104 views
1

所以我在我的java作業中遇到了一個問題。其任務是編寫一個讀取十個數字的程序,並只顯示不同的數字以及不同的數值。我已經得到了迄今...讀取並存儲數組中的唯一值(JAVA)

import java.util.Scanner; 
import java.util.Collection; 

public class Exercise06_05 { 
    public static void main(String[]args){ 
     Scanner input = new Scanner(System.in); 
     int[] list = new int[10];//create my new 10 slot array 
     //i really want a variable length array but alas 

     for (int i = 0; i < 10; i++){//get and check the ten input variables 
      System.out.println("Enter an Integer");//ask for input 
      int integer = input.nextInt();//assign the input to a temp variable 
      if (isUnique(list, integer)){//check if the temp is unique to the array 
       list[i] = integer;//if so assign it 
      } 
     } 



     String output = ""; 
     int j = 0; 
     for (j = 0; j < list.length; j++){ 
      if(list[j] != 0){//this is where the error accours 
        output += (list[j] + " "); 
      }else 
       break;//this break ensures J doesn't get any higher 
      //so that i can plug that in for the number of distinct variables 
     } 
     System.out.println("The number of distinct numbers is " + j); 
     System.out.println(output);//print output, and the number of distinct values 


    } 
      public static boolean isUnique(int [] arry, int a){// my masterpiece of a method 
      for (int i = 0; i < (10);){ 
       if (arry [i] == a){//check box 
       return false;//not unique 
      } else if (i == (arry.length - 1)){//we done yet? 
       return true;//if so, return that it's unique 
      }else//if we're not done increment the box 
       i++;//there that is 
      } return false;//now put this here just to safeguard 


     } 

    } 

它工作正常,除非用戶再次輸入兩個像1行相同的整數,然後1。發生什麼事是程序不存儲第二個int,該數組保持爲零,然後在創建輸出部分失敗。我如何解決這個問題?

+0

如果您的要求顯示不同的數字,爲什麼還需要顯示唯一數字的數量?這似乎很奇怪,也許是對這個問題的誤解?另外,請看一下sets(http://download.oracle.com/javase/1.5.0/docs/api/index.html?java/util/Set.html),它們基本上是獨特的東西...... – 2011-04-27 17:38:10

回答

0

那麼,首先,如果您獲得的數字是唯一的,則將其插入到數組中。如果它不是唯一的,則不要將其插入到數組中,但仍然前進數組索引。因此,您將使初始化默認值爲零。

這有兩個問題。

  1. 沒有辦法區分「重複值」和「零」。如果零不是合法的價值,那麼好吧。如果零是合法的,這是行不通的。

  2. 更重要的是,當您在輸出中循環訪問數組時,當您點擊第一個零時退出。因此,如果用戶輸入了1,2,4,2,3,4,那麼您需要用1,2,4,0,3,0填充陣列。然後當你顯示你要寫1,2,4的輸出時,看到零並退出,並聲明有3個唯一值。你永遠不會達到3.

我不知道他們教了你多少關於可用的數據結構呢。更好的解決方案是使用ArrayList而不是數組,然後只在傳入值唯一時才添加到ArrayList。如果你還沒有了解這一點,另一個想法就是隨時跟蹤唯一值的數量。當你得到一個重複的值時,不要插入到數組中,也不要增加計數器。也就是說,有一個計數器就是arrray中的位置,它與唯一值的數量相同。有另一個計數器是讀取輸入值的數量。一旦你看到第一個副本,這將會大於數組的位置。

在一個細節點上,與您的問題沒有直接關係:在您的isUnique函數中,爲什麼要循環到一個硬編碼的十,然後有一個單獨的IF語句來測試數組長度並在什麼時候中斷你到達最後?這將是一個非常簡單並且更易於閱讀,如果你編碼:

public static boolean isUnique(int [] arry, int a) 
{ 
    for (int i = 0; i < arry.length) 
    { 
    if (arry [i] == a) 
    { 
     return false; //not unique 
    } 
    } 
    return true; // we made it through without finding a dup, must be unique 
} 

或者,如果,正如我上面建議,你有一個獨立的變量來說實際上有多少數組的填充:

public static boolean isUnique(int [] arry, int filled, int a) 
{ 
    // filled should always be <=arry.length, but we can check both just to be safe 
    for (int i = 0; i < arry.length && i<filled) 
    { 
    if (arry [i] == a) 
    { 
     return false; //not unique 
    } 
    } 
    return true; // we made it through without finding a dup, must be unique 
} 
0

你的問題是你試圖輸出比數組中更多的值。

否則int[] list = new int[10]意味着list.length將總是10.

從陣列無論切換到列表中,或跟蹤插入的數量。

0

即使沒有存儲的值,使i遞增1

一種解決方案是:

if (isUnique(list, integer)){//check if the temp is unique to the array 
      list[i] = integer;//if so assign it 
     } 
else 
{ 
    list[i] = 0; 
} 

將在單元存儲0,否則沒有什麼(甚至不是0)單元格和程序崩潰。

相反,如果您想爲單元格獲得另一個值,請將i減1,而不是將單元格賦值爲0。

+0

謝謝,一個相當優雅的解決方案 – user727694 2011-04-27 17:07:59

2

好吧,這有點不同,它不是回答你的問題,你已經這樣做了,而是關注你的解決方案。您正在閱讀數字列表,然後對這些數字進行操作。 這裏是另一個片斷:

loop = 0; 

/**Start Reading your numbers from stdin or anything**/ 
while(There are numbers to read){ 

    int number = read_number(), counter = 0, found = 0; 

    while(counter <= loop){ 
     if(a[counter] == number){ 
      found = 1; // you found a match for this number break here 
      break; 
     } 

     counter++; 
    } 

    if(found == 0){ 
     /* We did not find our number in array*/ 
     a[counter] = number; 
    } 
} 

這就是它只讀一次,循環一次。

相關問題