这是一个非常基本的问题,但我刚刚开始使用JAVA,并且在数组方面遇到了一些打击。
我要做的是使用来自用户的6条信息填充数组:
到目前为止,我已将这些输入放入JAVA中的数组中,但我想要做的是使用相应的数字输入来选择Pay Scale数组中的常量,然后使用该常量来计算每个员工的工资。
例如,员工1在规模0下工作了10个小时,因此这将是10 * 4.50 和员工在规模1工作10小时,这将是10 * 4.62
import java.util.Arrays; //imports Array utility
import java.util.Scanner; //imports Scanner utility
public class test1 {
static Scanner keyb = new Scanner(System.in); //Adds a keyboard input
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of Employees: ");
int employees = scanner.nextInt();
String[] Employee = new String[employees];
String[] FirstName = new String[employees];
String[] LastName = new String[employees];
double[] HoursWorked = new double[employees];
double[] PayScale = {4.50,4.62,4.90,5.45,6.20};
for (int i = 0; i < Employee.length; i++) {
System.out.print("Enter Employee Number: ");
Employee[i] = scanner.next();
System.out.print("Enter Employee's First name: ");
FirstName[i] = scanner.next();
System.out.print("Enter Employee's Last name: ");
LastName[i] = scanner.next();
System.out.print("Enter Employee's Hours worked: ");
HoursWorked[i] = scanner.nextDouble();
System.out.print("Enter Employee's Payscale (Number 0 to 4): ");
PayScale[i] = scanner.nextDouble();
}
for (int i = 0; i < HoursWorked.length; i++) {
System.out.println("Employee " + Employee[i] + " " + FirstName[i] + " " + LastName[i] + " has "
+ HoursWorked[i] * PayScale[0]);
}
}
}
}
我是否接近解决方案?
我在JAVA中问的可能吗?
也许我只是以错误的方式看待这个问题,但是对此有任何帮助都会非常感激。
修改
好的,我将extras数组添加到代码中 扫描仪扫描仪=新扫描仪(System.in); System.out.println(“输入员工人数:”); int employees = scanner.nextInt(); String [] Employee = new String [employees]; String [] FirstName = new String [employees]; String [] LastName = new String [employees]; double [] HoursWorked = new double [employees]; int [] PayScale2 = {0,1,2,3,4}; double [] PayScale = {4.50,4.62,4.90,5.45,6.20};
我只是不确定我用
索引原始PayScale数组的位置PayScale的[PayScale2 [I]]
它会进入for语句代码块吗? (我试过把它放在那里然而我得到一个错误,它不是一个声明:/
答案 0 :(得分:1)
更改
+ HoursWorked[i] * PayScale[0]);
到
+ HoursWorked[i] * PayScale[i]);
除此之外,在我看来,就像你正在做的那样,你说你应该做的......
你已经拥有payscales
来自double[] PayScale = {4.50,4.62,4.90,5.45,6.20};
,所以以下内容并没有多大意义:
System.out.print("Enter Employee's Payscale (Number 0 to 4): ");
PayScale[i] = scanner.nextDouble();
首先,如果你想要keep this number (Number 0 to 4) separately
,你应该使用another Array
,not
保留Payscales
的那个,然后你可以index
first array
保留different rates
..或者你可以directly
使用第一个数组if you know
每个员工的薪资等级...最后它必须做你想做的事以及你想做什么,但逻辑和工具都在那里。例如If you call the 2nd array PayScale2
:
System.out.print("Enter Employee's Payscale (Number 0 to 4): ");
PayScale2[i] = scanner.nextDouble();
然后你可以index to the first array
例如:
PayScale[PayScale2[i]]
在这种情况下,如果用户输入0
,那么PayScale2[i]
将是0
,那么PayScale[PayScale2[i]]
将是PayScale[0]
或4.5
或者是您将值设置为等于第一个数组