如何编写交互式程序(参数和对象)
How to write an interactive program ( Parameters and objects)
在我的 CS class 中,我们遇到了以下问题:
Write a Java program that includes a method called pay that accepts two parameters: a real number for an employee's hourly wage and an integer for the number of hours the employee worked that day. The method should return how much money to pay the employee for this day.
例如调用pay(5.50, 6)
应该return33.0
。
对于超过 8
的任何时间,员工应获得 "overtime" 正常时薪 1.5
倍的工资。比如调用pay(4.00, 11)
应该return50.0
(计算为(4.00 * 8) + (6.00 *3)
).
在该程序的 main
方法中包含对 pay 方法的四个调用。两次调用,每次调用都使用上面给出的示例数据,还有两次调用,每次调用都使用您确定的数据。
到目前为止,这就是我所得到的,我觉得没什么:
import java.util.*;
public class Payjava {
//obtain values
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println(" Wage : ");
double wage = console.nextDouble();
System.out.print(" Hours :");
int hours = console.nextInt();
// compute results
double overtime = 1.5;
double overtime = hours > 8 * (wage * 1.5);
wage = hours * wage;
}
}
我建议首先将问题分解成更小的部分。
这个问题要求一种计算员工工资的方法,所以这似乎是一个合理的起点。问题给了你方法的名称和它应该采用的参数:
...a method called pay that accepts two parameters: a real number for an employee's hourly wage and an integer for the number of hours the employee worked that day. The method should return how much money to pay the employee for this day.
所以你的方法应该是这样的:
public double pay(double hourlyWage, int numberOfHours) {
//TODO: complete
}
然后开始将逻辑添加到方法中 - 从没有超时的简单情况开始,然后返回并添加超时计算。
最后,请注意问题说要在 main 方法中包含对 pay 方法的四次调用 - 因此您实际上不需要提示输入。
答案是这样的,
public class PayJava {
public static void main(String[] argv) {
PayJava test = new PayJava();
double pay = test.pay(5.2,5);
System.out.println("Pay 1= " + pay);
pay = test.pay(4.00, 11);
System.out.println("Pay 2= " + pay);
pay = test.pay(5.50, 6);
System.out.println("Pay 3= " + pay);
pay = test.pay(7.50, 14);
System.out.println("Pay 4= " + pay);
}
private double pay(double wage , int hours) {
double overtime= 1.5;
int otHrs = 0;
int regHrs = 0;
if(hours > 8 ) {
regHrs = 8;
otHrs = hours - regHrs;
overtime = (wage*1.5)*otHrs;
} else {
regHrs = hours;
overtime = 0;
}
wage = (wage*regHrs)+overtime;
return wage;
}
}
在我的 CS class 中,我们遇到了以下问题:
Write a Java program that includes a method called pay that accepts two parameters: a real number for an employee's hourly wage and an integer for the number of hours the employee worked that day. The method should return how much money to pay the employee for this day.
例如调用pay(5.50, 6)
应该return33.0
。
对于超过 8
的任何时间,员工应获得 "overtime" 正常时薪 1.5
倍的工资。比如调用pay(4.00, 11)
应该return50.0
(计算为(4.00 * 8) + (6.00 *3)
).
在该程序的 main
方法中包含对 pay 方法的四个调用。两次调用,每次调用都使用上面给出的示例数据,还有两次调用,每次调用都使用您确定的数据。
到目前为止,这就是我所得到的,我觉得没什么:
import java.util.*;
public class Payjava {
//obtain values
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println(" Wage : ");
double wage = console.nextDouble();
System.out.print(" Hours :");
int hours = console.nextInt();
// compute results
double overtime = 1.5;
double overtime = hours > 8 * (wage * 1.5);
wage = hours * wage;
}
}
我建议首先将问题分解成更小的部分。 这个问题要求一种计算员工工资的方法,所以这似乎是一个合理的起点。问题给了你方法的名称和它应该采用的参数:
...a method called pay that accepts two parameters: a real number for an employee's hourly wage and an integer for the number of hours the employee worked that day. The method should return how much money to pay the employee for this day.
所以你的方法应该是这样的:
public double pay(double hourlyWage, int numberOfHours) {
//TODO: complete
}
然后开始将逻辑添加到方法中 - 从没有超时的简单情况开始,然后返回并添加超时计算。
最后,请注意问题说要在 main 方法中包含对 pay 方法的四次调用 - 因此您实际上不需要提示输入。
答案是这样的,
public class PayJava {
public static void main(String[] argv) {
PayJava test = new PayJava();
double pay = test.pay(5.2,5);
System.out.println("Pay 1= " + pay);
pay = test.pay(4.00, 11);
System.out.println("Pay 2= " + pay);
pay = test.pay(5.50, 6);
System.out.println("Pay 3= " + pay);
pay = test.pay(7.50, 14);
System.out.println("Pay 4= " + pay);
}
private double pay(double wage , int hours) {
double overtime= 1.5;
int otHrs = 0;
int regHrs = 0;
if(hours > 8 ) {
regHrs = 8;
otHrs = hours - regHrs;
overtime = (wage*1.5)*otHrs;
} else {
regHrs = hours;
overtime = 0;
}
wage = (wage*regHrs)+overtime;
return wage;
}
}