2017-12-06 1 views
0

私は割り当てとして作業しているプログラムを持っています。要件の一部は、ユーザーから数字を取得して保存する方法を呼び出すよう求めていますそれらをサイズ20arrayに変換する。配列に重複する値を入れずにこれを行う必要があります。配列が一杯になるまで、またはユーザーが負の値を入力するまで、ユーザー番号の格納を続けます。私はいくつかの問題を抱えています。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20重複を無視してキーボードから配列にデータを保存する

Enter the integers for the array: 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 18 18 18 18 19 20

格納する必要があり:

  1. ユーザは、例えば、中断せずに(キーを「入力」を使用して)複数行のテキストを介してデータを入力することができなければなりません重複する値のない配列20の一意の数字が入力されていたため、プログラムは他の値を受け付けませんでした。 nextInt(),nextLine()などのさまざまなオプションを使用しようとしましたが、数字をリストとして保存して変換しても、それほど多くの問題があります。

は、だから私の質問は、どのように私は、ユーザーがキーボードバッファの現在の値が/重複負であるかどうかをチェックするだけでなく、入力の複数行にわたって値を入力することはできないので、私はそれをスキップすることを知ることができますバッファ内の値またはプログラムを終了しますか?

私は自分のコードを含めるつもりですが、正直言って私はこれを何度も繰り返して試してみました。どんな援助も歓迎される以上のものです。

public static int getData(int [] set){ 
    Scanner keyboard = new Scanner(System.in); 

    System.out.println("Enter the integers for the array."); 
    System.out.println("The program will stop accepting values when you enter a negative number or reach the 20 unique limit."); 

    int usrInput=keyboard.nextInt(), arraySizeUsed = 0; 

    for(int position = 0; position < 20;) { 

     if(!Arrays.asList(set).contains(usrInput)) { 
      if(usrInput > -1) { 

       set[position] = usrInput; 
       position++; 
       arraySizeUsed++; 
      } 

      else { 
       return arraySizeUsed; 
      } 
     } 
     else { 
      continue; 
     } 
    } 

    return arraySizeUsed; 
} 

は、私はちょうど、キーボードバッファの現在の数は、アレイ内に既に存在する場合にはインクリメントからの位置を維持したいと私はループ内で「続ける」を使用しなければならないとは思いません。私は何が欠けていますか?

+0

なぜ値をHashSetに格納せず、マイナスと20をセンチネルとして使用しますか。 –

答えて

0

int配列と直接比較検索を使用して、別のソリューションです。

import java.util.Scanner; 

public class NumberScanner { 

    private static final int MAX_SIZE = 20; 

    static int[] intArray = new int[MAX_SIZE]; 

    public static void main(String[] args) { 

     Scanner keyboard = new Scanner(System.in); 

     int value = 0; 
     int size = 0; // Not an array counter, starts at 1. 0 means empty. 

     System.out.println("Enter a series of positive integers on one or more lines. Enter a negative number to quit."); 

     // Loop until a negative number is found or the MAX_SIZE is reached 
     while (size < MAX_SIZE) { 
      value = keyboard.nextInt(); 
      if (value < 0) 
       break; 

      boolean found = false; 
      if (value >= 0 && size > 0) { 
       // Add the value to the array 
       // First, check if its already there 
       for (int i = 0; i<size; i++) { 
        if (intArray[i] == value) { 
         found = true; 
         break; // Stop the loop 
        }     
       } 
      } 
      if (!found) {    
       intArray[size] = value; 
       size++; 
      } 
     } 

     for (int i=0; i<size; i++) { 
      System.out.printf(intArray[i] + " "); 
     } 
     System.out.printf("\n"); 

     keyboard.close(); 
    } 
} 
+0

に関係なく配列にそれらを追加し続けます。私が必要としたものと正確なもの、ありがとう – cryingAtMyKeyboard

0

このようなものを試してみてください。ここで

int position = 0; 
while (position < 20) { 
    int next = keyboard.nextInt(); 
    if (next < 0) { 
     break; // Negative so exit loop and return 
    } 

    if (!ArrayUtils.contains(set, next)) { 
     set[position] = next; 
     position++; 
    } 

return set; 
+0

お世話になりました。何らかの理由で、このコードは重複を考慮せず、 – cryingAtMyKeyboard

1
Set<Integer> values = new HashSet<>(); 
    Scanner sc = new Scanner(System.in); 
    int count = 0; 
    do { 
     int in = sc.nextInt(); 
     if(in>0 && count < 20) { 
      values.add(in); 
     }else { 
      break; 
     } 
    }while(sc.hasNext()); 
関連する問題