gpt4 book ai didi

java - 方法不打印java中的所有值

转载 作者:行者123 更新时间:2023-12-01 12:25:06 24 4
gpt4 key购买 nike

我有2个java程序,第一个程序从用户那里获取有关员工的所有信息(id,姓名,部门等)并打印出来,第二个程序让用户选择有多少员工,然后需要像(员工ID,姓名等)这样的值,这个程序使用一个数组来存储值,它应该打印不同员工的所有值,但是当我运行程序时,第二组值覆盖第一组值并打印第二组值两次,我是初学者,所以请帮助

这是第一个程序

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);
}

}

}

最佳答案

这是有问题的代码区域:

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% 语法正确,但非常接近。

关于java - 方法不打印java中的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26425082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com