2016-08-20 16 views
1

現在、私はJavaプログラミングクラスの課題に取り組んでいます。私は自分自身を少しバインドしているようだ。私が間違っていることを理解するのを助ける助けがあれば、大変感謝しています。スクールの割り当て多次元配列の問題

割り当て

は、次のことを行いプログラム書く:

は多次元配列

に以下のデータを入れて、彼らが従業員の給与の統計情報をご希望の企業をユーザーに求めます。

平均従業員の給与を倍数として返す方法を記述します。このメソッドに会社番号と従業員の賃金を渡します。

総従業員給与をintとして返すメソッドを記述します。このメソッドに会社番号と従業員の賃金を渡します。

従業員数をintとして返すメソッドを記述します。このメソッドに会社番号と従業員の賃金を渡します。

メインメソッドでは、他のメソッドを呼び出して結果を出力します。

私はまだ新しく、プログラミングの原則のいくつかを理解するのに苦労しています。

私は、私が代わりにメソッドの計算の位置を取得していますプログラム(悪い出力)を実行すると:

bad output

ここ

私がこれまで持っているものです。

package salaries; 

import java.util.Scanner; 

public class Salaries { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     //declare, instantiate, and define value of multi array [3] [12] 
     double [][] mSalary = { { 49920, 50831, 39430, 54697, 41751, 36110, 
           41928, 48460, 39714, 49271, 51713, 38903}, 
          { 45519, 47373, 36824, 51229, 36966, 40332, 
           53294, 44907, 36050, 51574, 39758, 53847}, 
          { 54619, 48339, 44260, 44390, 39732, 44073, 
           53308, 35459, 52448, 38364, 39990, 47373}}; 

     //declare, instantiate, and define value 
     //of single array for company names 
     //and output values to user for selection 
     String [] company = { "Alhermit", "Logway", "Felter" }; 
     for(int i = 0; i < company.length; i++) 
      System.out.println("Company " + i + " : " +company[i]); 

     Scanner scan = new Scanner(System.in); 
     int cCompany; 
     do{ 
      //ouput for user to select a company 
      System.out.print("Select company: (0)" +company[0]+ ", (1)" 
          +company[1]+ "; (2)" +company[2]+ " > "); 
      //scan user input into cCompany 
      cCompany = scan.nextInt(); 


      //call number method 
      num nums = new num(); 
      nums.number(mSalary, cCompany); 

      //call total method 
      total sum = new total(); 
      sum.total(mSalary, cCompany); 

      //call average method 
      avg cAvg = new avg(); 
      cAvg.average(mSalary, cCompany); 


      //output statistics to user on selected company 
      System.out.println("You have selected the company " + company[cCompany] + ". "); 
      System.out.println(company[cCompany] + " has " + nums + " of employees."); 
      System.out.println("A total employee salary of " + sum + "."); 
      System.out.println("The average employee salary is " + cAvg); 
     } 
      while(cCompany < 0 || cCompany > 2); 
    } 
} 

//total class to calculate 
//salary of user selected company 
class total { 

    public static int total(double [][] mSalary, int cCompany){ 

     //assign variables 
     int sum = 0; 

     //for loop to calculate salary total of user input company 
     for(int j = 0; j < mSalary[cCompany].length; j++){ 
      sum += mSalary[cCompany][j]; 

     } 

    //return statement 
    return sum; 
    } 
} 

//average class to calculate 
//average of user selected company 
class avg { 

    public static double average(double [][] mSalary, int cCompany){ 

     //assign variables 
     int cAvg = 0; 
     int sum = 0; 
     int count = 0; 

     //totals the values for the selected company by 
     //iterating through the array with count. 
     while(count < mSalary[cCompany].length){ 
      sum += mSalary[cCompany][count]; 
      count +=1; 
     } 

      cAvg = sum/mSalary[cCompany].length; 
      return cAvg; 
    } 
} 
//number class to calculate amount of 
//employees in user selected company 
class num { 

    public static int number(double [][] mSalary, int cCompany){ 

     //assign variables 
     int nums = 0; 

     //number of employees based on length of colomn 
     nums = mSalary[cCompany].length; 
     return nums; 
    } 
} 
+0

あなたは「悪い出力」の画像にリンクします(画像は使用せず、あなたの質問にテキストとして含める)が、「正しい出力」とは言わなかった。あなたが現在立ち退いている任務のどの段階でもない。 –

+0

私は、不適切なクラス名、間違った字下げ、矛盾した空白など、たくさんのスタイルの問題があることがわかります。 –

答えて

1

numssum、とcAvgは、あなたが持っているクラスのすべてのインスタンスです、あなたはそれらのクラスのインスタンスをプリントアウトしています。

(ちなみに - あなたはこれらのクラスの名前を変更する必要がありますクラスは大文字で始めるそれは変数からそれらを区別します。。。)

これで間違って二つのものがあります。

  1. あなたはデータなしが含まれており、何のtoString方法を持っていないクラスをインスタンス化しています。
  2. データを返す静的メソッドのみを持つクラスをインスタンス化しています。クラスをインスタンス化する必要はありませんすべて;代わりに、メソッド呼び出しの結果を表示するだけです。

    System.out.println(company[cCompany] + " has " + num.number(mSalary, cCompany); + " of employees."); 
    

    私は読者の練習として、残りの部分を残して:ような何かにこれらの呼び出しの少なくとも一つを変更します

+0

あなたの返事をありがとう、それは私がする必要があった正確に私を得た。私は静的メソッドをインスタンス化するために不要なコードを削除し、あなたが示したように出力ステートメントを置き換えました。クラス名で問題を修正して大文字にしました。すべてがうまくいっています。あなたの返信はすばらしく、大きな助けとなりました。ありがとうございました!! – Cezpool

+0

私はそれが働いた後、私は戻って、3つのクラスの宣言を削除し、私のメソッドコードをメインクラスの中に置き、変更を反映するように出力ステートメントを変更しました。コードが少なく、処理が少なく、よりクリーンに見えます。 – Cezpool

関連する問題