2017-01-30 9 views
1

私はJavaプログラミングとオブジェクトプログラミング全体に新しいので、どのタイプの配列を使用するのか困っています。使用する配列の種類を決定する

NAMEとPRICEの両方のユーザー入力を受け入れ、配列に格納する配列を作成する必要があります。私は2つの別々のアレイを作って、カウンターを使ってみましたが、それは私のニーズに合っていません。問題は、特定の名前が入力された場合、配列を逆にして配列をフィルタリングするだけでなく、PRICEを別々に平均化できる必要があるという問題にあります。私はオブジェクト配列の作成も試みましたが、それが適切かどうかはわかりませんでした。ここで

が私のコードです:私は必ずしもそのような私のためにこれを行うために誰かまたは何かを探していないよ

String names[] = new String[100]; // sets array size to almost ulimited # 
      double prices[] = new double[100]; // sets array size to almost unlimited # 
      double average = 0; // initializes the array 
      int count = 0; // initializes the count for the arraysize 
      boolean flag = false;// initializes flag to false so it can be changed later 


      while(true){ 
      System.out.print("Enter item " + (count + 1) + " name: "); // enter name 
      names[count] = in.next(); // next string becomes count index 
      if(names[count].equals("-1")){ break;} // if the next name entered is sentinel break 
      System.out.print("Enter item " + (count + 1) + " price: "); // enter price 
      prices[count] = in.nextDouble(); // stores price entered as array index 
      // if next price == -1 then the program ends 
      if(names[count].equalsIgnoreCase("peas")) flag = true; 
      average += prices[count]; 
      count++; 

     } 
      System.out.println("The items and their prices are: "); 
      for(int i = 0; i < count; i++){ 
      System.out.println(names[i] + ": " + formatter.format(prices[i])); 
     } 
      average /= count; 
      if(flag == true){ 
      System.out.println("Average: " + formatter.format(average)); 
     } 
      else{ 
       System.out.println("no average output"); 
     } 

、私は単純に困惑し、私のロジックを考えなければなりませんどの方法を探しています方向。みなさん、ありがとうございました!

+0

みなさそれをすべて-put 1つのループで はを取り除く。-GETこれはどういう意味ですか?あなたのコードには実際に問題がありますか、単にコードレビューを探していますか? –

+0

ご迷惑をおかけして申し訳ございませんが、基本的に価格と名前を1つの配列に保存する必要がありますが、メソッドを使用して価格を平均化できる必要があります。私はオブジェクト配列に格納しようとし、オブジェクト配列からの価格を平均化できませんでした。 – MonkeyWithMachine

+0

なぜ配列ですか? Map を使用することができます –

答えて

1

あなたのループは基本的にはうまくいくと思います。 「平均が真のがあれば、」平均値が正確であるだけで何とか `私が何をするか...価格のseparately`を平均化できるようにする必要があります偽

String names[] = new String[5]; // sets array size to almost unlimited # 
      double prices[] = new double[100]; // sets array size to almost unlimited # 
      double average = 0; // initializes the array 
      int count = 0; // initializes the count for the arraysize 
      boolean flag = false;// initializes flag to false so it can be changed later 


      while(true){ 
      System.out.print("Enter item " + (count + 1) + " name: "); // enter name 
      names[count] = System.console().readLine();//in.next(); // next string becomes count index 
      if(names[count].equals("-1")){ break;} // if the next name entered is sentinel break 
      System.out.print("Enter item " + (count + 1) + " price: "); // enter price 
      prices[count] = Double.valueOf(System.console().readLine());//in.nextDouble(); // stores price entered as array index 
      // if next price == -1 then the program ends 
      if(names[count].equalsIgnoreCase("peas")) flag = true; 
      average += prices[count]; 
      count++; 
//  }//deleted 

      System.out.println("The items and their prices are: "); 
      for(int i = 0; i < count; i++){ 
      System.out.println(names[i] + ": " + prices[i]);//formatter.format() 
//  }//deleted 
      average /= count; 
      //if(flag == true){ 
      System.out.println("Average: " + average);//formatter.format() 
     //} 
      else{ 
       System.out.println("no average output"); 
     }//added 
     }//added 
     } 
+0

ありがとう、私はこれを試してみます。 – MonkeyWithMachine

4

まず、NamePriceのフィールドを持つクラスを作成することをお勧めします。

public class Product { 
    String name, price; 

    //Getter and Setter 
    // You can also define some other methods here as well. 
} 

このクラスのArrayListを作成します。

ArrayList<Product> myProduct = new ArrayList(); 

この配列のすべての処理を行います。

+0

ありがとう、これは私のテストクラスの一部です。私はarrayListsを実際に使用していませんが、今私はそれらを研究します。再度、感謝します。 – MonkeyWithMachine

関連する問題