这个问题很长,但我希望我提供的信息足够。除了我必须构造一个BaysittingJob
对象并显示其值的那一点之外,所有代码都有效。太感谢了。
我无法让应用程序输出所需的结果。它应该:
- 显示具有两位数字年份和四位数字序列号的工作编号
- 根据选择的保姆代码(代码从 1 到 3)分配的保姆
- 孩子数
- 工作小时数
- 根据工作小时数和选择的保姆收取费用 如果保姆代码为 1,则每小时收费 7 美元 如果保姆代码为 2 或 3,则第一个孩子的费用为每小时 9 美元,每增加一个孩子每小时 4 美元孩子
BabysittingJob
public class BabysittingJob {
private int jobNumber;
private int babysitterCode;
private String babysitter;
private int childrenNumber;
private int hoursWorked;
private double fee;
public BabysittingJob(int year, int sequentialNumber, int babysitterCode, int childrenNumber, int hoursWorked, String babysitter) {
year = year % 2000;
int jobNumber = ((year * 10000) + sequentialNumber);
if (babysitterCode == 1) {
babysitter = "Cindy";
} else if (babysitterCode == 2) {
babysitter = "Greg";
} else if (babysitterCode == 3) {
babysitter = "Marcia";
}
}
{
if (babysitterCode == 1) {
fee = hoursWorked * 7 * childrenNumber;
} else if (babysitterCode > 1 && childrenNumber == 1) {
fee = hoursWorked * 9 * childrenNumber;
} else if (babysitterCode > 1 && childrenNumber > 1) {
fee = (hoursWorked * 9) + ((--childrenNumber) * hoursWorked * 4);
}
}
public void displayResult() {
System.out.println("Your job number is:" + jobNumber + "Your baby sitter is:" + babysitter
+ "The number of children is:" + childrenNumber + "The number of hours worked is:"
+ hoursWorked + "Your fee is $:" + fee);
}
}
CreateBabysittingJob
...
import java.util.Scanner;
public class CreateBabysittingJob {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter year.");
int year = input.nextInt();
while (year < 2013 || year > 2025) {
System.out.println("Enter a year between 2013 and 2025");
year = input.nextInt();
}
System.out.print("Enter job number");
int jobNumber = input.nextInt();
while (jobNumber < 1 || jobNumber > 9999) {
System.out.println("Enter a number between 1 and 9999");
jobNumber = input.nextInt();
}
System.out.print("Enter babysitter code");
int babysitterCode = input.nextInt();
while (babysitterCode < 1 || babysitterCode > 3) {
System.out.println("Enter a number between 1 and 3");
babysitterCode = input.nextInt();
}
System.out.print("Enter number of children");
int childrenNumber = input.nextInt();
while (childrenNumber < 1 || childrenNumber > 9) {
System.out.println("Enter a number between 1 and 9");
childrenNumber = input.nextInt();
}
System.out.print("Enter number of hours");
int hoursWorked = input.nextInt();
while (hoursWorked < 1 || hoursWorked > 12) {
System.out.println("Enter a number between 1 and 12");
hoursWorked = input.nextInt();
}
BabysittingJob ca = new BabysittingJob(year, jobNumber, babysitterCode, childrenNumber, hoursWorked);
ca.displayResult();
}
}