Java 学校作业多维数组问题
我目前正在为我的Java编程类做作业。我似乎陷入了困境。任何帮助我认识到我做错了什么的人都将不胜感激 分配 编写一个执行以下操作的程序: 将下面的数据放入多维数组中 提示用户输入他们想要的公司员工工资统计信息 编写一个方法,以双倍形式返回员工的平均工资。将公司编号和员工工资传递到此方法 编写一个以整数形式返回员工总工资的方法。将公司编号和员工工资传递给此方法 编写一个将雇员人数返回为整数的方法。将公司编号和雇员工资传递给此方法 在main方法中调用其他方法并输出结果 请记住,我还是个新手,正在努力理解编程的一些原则 当我运行程序时,我得到的是位置,而不是方法计算(错误输出): 以下是我到目前为止的情况:Java 学校作业多维数组问题,java,arrays,multidimensional-array,methods,call,Java,Arrays,Multidimensional Array,Methods,Call,我目前正在为我的Java编程类做作业。我似乎陷入了困境。任何帮助我认识到我做错了什么的人都将不胜感激 分配 编写一个执行以下操作的程序: 将下面的数据放入多维数组中 提示用户输入他们想要的公司员工工资统计信息 编写一个方法,以双倍形式返回员工的平均工资。将公司编号和员工工资传递到此方法 编写一个以整数形式返回员工总工资的方法。将公司编号和员工工资传递给此方法 编写一个将雇员人数返回为整数的方法。将公司编号和雇员工资传递给此方法 在main方法中调用其他方法并输出结果 请记住,我还是个新手,正在努
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.util.Scanner;
公营工资{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//声明、实例化和定义多数组[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}};
//声明、实例化和定义值
//公司名称的单个数组
//并将值输出给用户进行选择
字符串[]公司={“Alhermit”、“Logway”、“Felter”};
对于(int i=0;i”;
//将用户输入扫描到公司
cCompany=scan.nextInt();
//呼叫号码法
num nums=新num();
编号(澳门特别行政区,公司);
//调用总数法
总额=新总额();
总额(澳门特别行政区、公司);
//呼叫平均法
avg cAvg=新的平均值();
平均费用(澳门特别行政区、公司);
//将所选公司的统计信息输出给用户
System.out.println(“您已选择公司”+公司[cCompany]+”;
System.out.println(公司[公司]+“拥有”+nums+“员工”);
System.out.println(“员工总工资“+sum+”);
System.out.println(“员工平均工资为”+cAvg);
}
而(公司<0 | |公司>2);
}
}
//要计算的总类数
//用户选择的公司的薪资
班级总数{
公共静态整数总计(双倍[][]人民币,国际公司){
//分配变量
整数和=0;
//用于计算用户输入公司工资总额的for循环
对于(int j=0;jnums
、sum
和cAvg
都是您拥有的类的实例,您正在打印这些类的实例
(顺便说一下,您应该更改这些类的名称。类以大写字母开头。它将它们与变量区分开来。)
这有两个问题
您正在实例化一个不包含数据且没有toString
方法的类
您正在实例化一个只有静态方法返回数据的类。你根本不需要实例化这个类;相反,只需打印方法调用的结果
这会将这些调用中的至少一个更改为:
System.out.println( company[cCompany] + " has " + num.number(mSalary, cCompany); + " of employees." );
我把剩下的留给读者作为练习。你链接到一个“错误输出”的图像(不要使用图像;在你的问题中包含为文本),但你没有说什么是“正确输出”;也不知道你目前在作业的哪个阶段。我可以看到很多风格问题,包括不正确的类名、不正确的缩进、不一致的空格。谢谢你的回复,它让我找到了我需要的地方。我删除了不必要的代码来实例化静态方法,并替换了输出语句以反映您所展示的情况。我也已经用英语纠正了这个问题