Java 输出值总是返回不正确的内容
我目前正在为我的项目编写代码,我似乎把所有东西都记下来了,除了我的总工资结果总是为0.0,我不知道在哪里或如何修复它。这是我的密码,应该打印出来 有多少员工?一, 工作、工资率和员工姓名之间用空格分隔:37.25 15.5 Tony Stark 员工姓名:托尼·斯塔克 工作时间:37.25小时Java 输出值总是返回不正确的内容,java,Java,我目前正在为我的项目编写代码,我似乎把所有东西都记下来了,除了我的总工资结果总是为0.0,我不知道在哪里或如何修复它。这是我的密码,应该打印出来 有多少员工?一, 工作、工资率和员工姓名之间用空格分隔:37.25 15.5 Tony Stark 员工姓名:托尼·斯塔克 工作时间:37.25小时 工资率:15.50 工资总额:577.38美元 public class Project3 { public static void main(String[] theArgs) { Scanner
工资率: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;
}
}
公共类项目3{
公共静态void main(字符串[]字符){
扫描仪控制台=新扫描仪(System.in);
双倍小时=0.0;
双倍工资率=0.0;
int employeeCount=getEmployeeCnt(控制台);
double grossPay=getGrossPay(工资率,小时);
processEmployeePay(控制台、employeeCount、grossPay、小时数、工资率);
}
公共静态int getEmployeeCnt(扫描仪控制台){
系统输出打印(“有多少员工?”);
返回console.nextInt();
}
公共静态字符串进程EmployeePay(控制台,int
员工人数、双倍工资、双倍工时、双倍工资率){
字符串empFirst=“”;
字符串empLast=“”;
对于(int i=1;i=48){
结果=(2*小时*工资率);
}否则{
若有(40小时){
结果=(1.5*小时*工资率);
}
否则{
如果(hours使您的变量类级别,因为它是本地的,所以它有默认值。
而在processEmployeePay
中设置值不会设置该值,因为java是按值传递的
看
更改如下:
class Your-Class{
private static double payRate = 0.0
...
Other fields
public static processEmployeePay(Scanner sc){
payRate = sc.nextDouble();
// others too
}
}
我们要猜测您的代码吗?我添加了一张图片,但由于某些原因,它没有显示为文本,而不是图片。还要添加预期和实际输出。当您将任何数字与0
相乘时,您将得到0
作为答案。