gpt4 book ai didi

java - 输出值总是返回不正确的东西

转载 作者:行者123 更新时间:2023-12-01 18:36:21 26 4
gpt4 key购买 nike

我目前正在编写我的项目,除了我的总工资结果总是 0.0 之外,我似乎一切都已完成,而且我不知道在哪里或如何修复它。这些是我的代码,应该打印出来有多少员工? 1

工作时间、工资率和员工姓名以空格分隔:37.25 15.5 Tony Stark员工姓名:托尼·斯塔克工作时间:37.25
工资标准:15.50
总工资:$577.38

public class Project3 {
public static void main(String[] theArgs) {

Scanner console = new Scanner(System.in);
double hours = 0.0;
double payRate = 0.0;
int employeeCount = getEmployeeCnt(console);
double grossPay = getGrossPay(payRate, hours);
processEmployeePay(console, employeeCount, grossPay, hours, payRate);

}
public static int getEmployeeCnt(Scanner theConsole) {
System.out.print("How many employees are there? ");
return theConsole.nextInt();
}
public static String processEmployeePay(Scanner theConsole, int
employeeCount, double grossPay, double hours, double payRate) {
String empFirst = "";
String empLast = "";
for(int i = 1; i <= employeeCount; i++) {
System.out.print("Enter Hours Worked, Pay Rate, and Employee Name separated by a space: ");
hours = theConsole.nextDouble();
payRate = theConsole.nextDouble();
empFirst = theConsole.next();
empLast = theConsole.next();
System.out.println("\t Employee Name: " + "\t" + empFirst + " " + empLast);
System.out.println("\t Hours Worked: " + "\t" + hours);
System.out.println("\t Pay Rate: " + "\t" + payRate);
System.out.println("\t Gross Pay: " + "\t" + grossPay);
}
return "";
}
public static double getGrossPay(double payRate, double hours) {
double result = 0.0;
if (hours >= 48) {
result = (2 * hours * payRate);
} else{
if (hours <= 48) {
result = (1.5 * hours * payRate);
} else {
if (hours > 40) {
result = (1.5 * hours * payRate);
}
else {
if (hours <= 40) {
result = (hours * payRate);
}
}
}
}
return result;
}


}

最佳答案

使你的变量为类级别,因为它是本地的,它有默认值。并且在processEmployeePay中设置值不会设置该值,因为java是按值传递的。

参见java-is-strictly-pass-by-value

按如下方式更改:

class Your-Class{
private static double payRate = 0.0
...
Other fields

public static processEmployeePay(Scanner sc){
payRate = sc.nextDouble();
// others too
}

}

关于java - 输出值总是返回不正确的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60033546/

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