-2

这个问题很长,但我希望我提供的信息足够。除了我必须构造一个BaysittingJob对象并显示其值的那一点之外,所有代码都有效。太感谢了。

我无法让应用程序输出所需的结果。它应该:

  1. 显示具有两位数字年份和四位数字序列号的工作编号
  2. 根据选择的保姆代码(代码从 1 到 3)分配的保姆
  3. 孩子数
  4. 工作小时数
  5. 根据工作小时数和选择的保姆收取费用 如果保姆代码为 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();
    }
}
4

2 回答 2

0

我发现了许多特殊之处...

首先,您定义BabysittingJobas...的构造函数

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

但是您尝试使用它构造一个实例

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

这是缺少babysitter. 问题是,您实际上是babysitter根据构造函数内的值确定值babysitterCode...

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

问题是,babysitter正在隐藏同名的类实例字段,这意味着您分配给此变量的任何内容在构造函数之外都没有任何意义,将实例字段保留为null

另一个特点是你有下面的一段代码只是挂在那里不知道在哪里......

{
    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 BabysittingJob(int year, int sequentialNumber, int babysitterCode, int childrenNumber, int hoursWorked) {
    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);
    }
}

我认为这应该让你更接近你想要的

您需要解决的其他问题...

  • 您永远不会将参数分配给构造函数childrenNumber中的类实例字段childrenNumber...this.childrenNumber = childrenNumber
  • 您永远不会将参数分配给构造函数hoursWorked中的类实例字段hoursWorked...this.hoursWorked = hoursWorked
于 2013-10-09T03:28:29.010 回答
0

您需要将类构造函数参数分配给其私有变量:

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;
        this.childrenNumber = childrenNumber;
        this.hoursWorked = hoursWorked;
        this.babysitterCode = babysitterCode;
        this.jobNumber = ((year * 10000) + sequentialNumber);

        if (babysitterCode == 1) {
            this.babysitter = "Cindy";
        } else if (babysitterCode == 2) {
            this.babysitter = "Greg";
        } else if (babysitterCode == 3) {
            this.babysitter = "Marcia";
        }

        if (babysitterCode == 1) {
            this.fee = hoursWorked * 7 * childrenNumber;
        } else if (babysitterCode > 1 && childrenNumber == 1) {
            this.fee = hoursWorked * 9 * childrenNumber;
        } else if (babysitterCode > 1 && childrenNumber > 1) {
            this.fee = (hoursWorked * 9) + ((--childrenNumber) * hoursWorked * 4);
        }
    }
}
于 2013-10-09T03:36:39.010 回答