【发布时间】:2016-12-27 20:01:13
【问题描述】:
我目前正在为我的 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;
}
}
【问题讨论】:
-
您链接到“错误输出”的图像(不要使用图像;在您的问题中包含为文本),但您没有说“正确输出”是什么;也不知道您目前处于作业的哪个阶段。
-
我可以看到很多样式问题,包括不正确的类名、不正确的缩进、不一致的空格。
标签: java arrays multidimensional-array methods call