我目前正在爲我的Java編程課程進行作業。我似乎讓自己陷入了一絲困境。任何幫助我意識到我做錯了什麼的幫助將不勝感激。學校作業多維陣列故障
分配
編寫一個程序,執行以下操作:
把下面的數據到一個多維數組
提示爲公司,他們希望員工工資統計數據的用戶。
編寫一個方法,將平均員工工資作爲雙倍返回。將公司編號和員工工資傳遞給此方法。
編寫一個方法,以int形式返回員工總工資。將公司編號和員工工資傳遞給此方法。
編寫一個方法,以int形式返回僱員數。將公司編號和員工工資傳遞給此方法。
在主要方法中調用其他方法並輸出結果。
請記住,我還是一個新手,並努力去理解編程的一些原理。
當我跑我得到的位置,而不是方法計算程序(壞輸出):
這是我到目前爲止有:
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;
}
}
您可以鏈接到「輸出錯誤」的圖片(不要使用圖片;在您的問題中包含文本),但您沒有說出「正確的輸出」是什麼;也不是你目前堅持的任務階段。 –
我可以看到很多樣式問題,包括不正確的類名,不正確的縮進,不一致的空白。 –