-3

编译时不断收到此消息:构造函数BabysittingJob(int, int, int, int, int)未定义。我正在尝试创建一个保姆应用程序。这个问题很长,但我希望我提供的信息足够。除了我必须构造一个 BaysittingJob 对象并显示其值之外,所有代码都有效。太感谢了。

我无法让应用程序输出所需的结果。它应该是: 1. 显示一个工作编号,其中有两位年份和一个四位序列号 2. 根据所选的保姆代码(代码从 1 到 3)分配的保姆 3. 孩子的数量 4 . 工作小时数 5. 根据工作小时数和选择的保姆收取费用 如果保姆代码为 1,则每小时收费 7 美元 如果保姆代码为 2 或 3,则第一个孩子的费用为每小时 9 美元每增加一个孩子,每小时增加 4 美元

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


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

1 回答 1

1

您的 BabysittingJob 构造函数接受 int、int、int、int、int、String:

 public BabysittingJob (int year, int sequentialNumber, int babysitterCode, 
     int childrenNumber, int hoursWorked, String babysitter)  {

你试图用 int、int、int、int 调用它,但你没有传入字符串:

BabysittingJob ca = new BabysittingJob(year, jobNumber, babysitterCode, 
     childrenNumber, hoursWorked);

解决方法:要么在调用构造函数时传入final String参数,要么新建一个不需要final String参数的构造函数。

于 2013-10-09T02:15:34.140 回答