我有2个java程序,第一个从用户获取有关员工的所有信息(id,名称,部门等)并打印出来,第二个程序让用户选择有多少员工然后需要像(雇用id,name等)这样的值,这个程序使用一个数组来存储值,它应该打印不同员工的所有值,但是当我运行程序时,第二组值会覆盖第一组值并打印出来第二组值两次,我是一个初学者,所以plse帮助
这是第一个程序
import java.util.Scanner;
public class payroll2
{
public static void main(String args[])
{
payroll2 payroll = new payroll2();
payroll.SetPayrollDetail();
payroll.SetBonus();
payroll.SetCommission();
payroll.SetNssf();
payroll.SetNetSalary();
payroll.GetPayroll();
}
Scanner myScanner=new Scanner(System.in);
int empID;
String empName;
String empDept;
String designation;
int basicSalary;
double bonus;
double commission;
double nssf;
double netSalary;
public void SetPayrollDetail()
{
System.out.println("Enter ID: ");
empID = myScanner.nextInt();
System.out.println("Enter Name: ");
empName = myScanner.next();
System.out.println("Enter Department (Marketing or Other): ");
empDept = myScanner.next();
System.out.println("Enter Designation (Manager, Executive or Other): ");
designation = myScanner.next();
System.out.println("Enter Basic Salary: ");
basicSalary = myScanner.nextInt();
}
public void SetBonus()
{
if(basicSalary < 1500){
bonus = 0.0;
}
else if(basicSalary>=1500 && basicSalary<3000){
bonus = basicSalary * (12.0/100.0);
}
else if(basicSalary>=3000 && basicSalary<5000){
bonus = basicSalary * (15.0/100.0);
}
else{
bonus = basicSalary * (25.0/100.0);
}
}
public void SetCommission()
{
if( empDept.equalsIgnoreCase("other") ){
commission = 0.0;
}
else if( empDept.equalsIgnoreCase("marketing") && designation.equalsIgnoreCase("manager") ){
commission = basicSalary * (30.0/100.0);
}
else if( empDept.equalsIgnoreCase("marketing") && designation.equalsIgnoreCase("executive") ){
commission = basicSalary * (15.0/100.0);
}
else if( empDept.equalsIgnoreCase("marketing") && designation.equalsIgnoreCase("other") ){
commission = basicSalary * (10.0/100.0);
}
else{
commission = 0.0;
}
}
public void SetNssf()
{
if(basicSalary < 1500){
nssf = basicSalary * (5.0/100.0);
}
else if(basicSalary>=1500 && basicSalary<3000){
nssf = basicSalary * (8.0/100.0);
}
else if(basicSalary>=3000 && basicSalary<5000){
nssf = basicSalary * (12.0/100.0);
}
else if(basicSalary>=5000 && basicSalary<7000){
nssf = basicSalary * (15.0/100.0);
}
else if(basicSalary>=7000 && basicSalary<10000){
nssf = basicSalary * (20.0/100.0);
}
else{
nssf = basicSalary * (25.0/100.0);
}
}
public void SetNetSalary()
{
netSalary = ( basicSalary + commission + bonus ) - nssf;
}
public void GetPayroll()
{
System.out.println("\n\nPayroll Details \n _____________________");
System.out.println("ID:\t\t" + empID);
System.out.println("name:\t\t" + empName);
System.out.println("Bonus:\t\t" + bonus);
System.out.println("Commission:\t"+commission);
System.out.println("NSSF:\t\t"+nssf);
System.out.println("Net Salary:\t"+netSalary);
}
}
这是第二个程序
import java.util.Scanner;
public class display{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
int counter;
int limit;
System.out.println("How many employess do u want to enter?\n Enter here: ");
limit = scan.nextInt();
int[] a = new int[limit];
payroll2 payroll = new payroll2();
for(counter=1; counter<=limit; counter++){
System.out.println("\n\nEnter employee "+counter+" details\n");
payroll.SetPayrollDetail();
payroll.SetBonus();
payroll.SetCommission();
payroll.SetNssf();
payroll.SetNetSalary();
}
for(counter=1; counter<=limit; counter++){
payroll.GetPayroll();
//System.out.println(a);
}
}
}
答案 0 :(得分:1)
这是代码中存在问题的区域:
for(counter=1; counter<=limit; counter++){
System.out.println("\n\nEnter employee "+counter+" details\n");
payroll.SetPayrollDetail();
payroll.SetBonus();
payroll.SetCommission();
payroll.SetNssf();
payroll.SetNetSalary();
}
for(counter=1; counter<=limit; counter++){
payroll.GetPayroll();
//System.out.println(a);
}
如果你考虑这是做什么的。因此,payroll2对象是单个实体。当您在第一个for循环中输入它们时,您将创建一个人。然后,如果你第二次或第三次循环,你就会覆盖那个人,因为你要保存到同一个变量。
然后当您循环第二个循环时,您正在打印工资核算实体,该实体保存最后输入的人员的值。由于其他数据被覆盖,因此您可以获得相同的印刷限制时间。
您要做的是构建一个工资单实体数组。此代码实现了这一点:
payroll2[] PayrollList = new payroll2[limit]; // establish the array with correct size
for(counter=0; counter<=limit - 1; counter++){ // bounds of the array are 0 to limit - 1
payroll = new payroll(); // hard reset of the variable to make sure data is cleared
System.out.println("\n\nEnter employee "+counter+" details\n");
payroll.SetPayrollDetail();
payroll.SetBonus();
payroll.SetCommission();
payroll.SetNssf();
payroll.SetNetSalary();
PayrollList[counter] = payroll; // adds it to the array at position counter
}
for(counter=0; counter<=limit - 1; counter++){
PayrollList[counter].GetPayroll(); // gets the payroll2 object from the array and calls its function
}
免责声明:我没有通过编译器运行修改后的代码,因此它可能不是100%语法正确,但它非常接近。