学习Java,对作业进行疑难解答

时间:2016-02-10 21:51:26

标签: java methods

所以我为我的一个课程分配了一个保险计算器,该计算器为年龄,教育程度,婚姻状况和就业状况提供一些输入,并使用它来提出保险金。默认付款设置为100,如果个人年龄小于22岁或超过70岁,则保险费增加25%。如果个人有学士学位,费用减少10%(大师为20%),如果个人结婚,则进一步减少20%。如果他们的就业状况是学生,成本增加10%,但如果他们失业,则减少5%。我有一大堆代码,过去几天我一直在使用它,我似乎陷入了条件限制。目前在两个类中运行,一个用于Individual.Class,另一个用于用户输入。

Individual.java:

public class Individual {
    private int age;
    private String ed_stat;
    private String mar_stat;
    private String emp_stat;
    private double default_Payment = 100;

    public Individual(int suns, String edu, String mar, String employ, double payments) {
        this.age = suns;
        this.ed_stat = edu;
        this.mar_stat = mar;
        this.emp_stat = employ;
        this.defaultPayment = payments;
    }

    public int getAge() {
        return this.age;
    }

    public String getEdu() {
        return this.ed_stat;
    }

    public String getMar() {
        return this.mar_stat;
    }

    public String getEmploy() {
        return this.emp_stat;
    }

    public double getPay(){
        if (22 < age > 70) {
            default_Payment = default_Payment + (default_Payment * .25);
        }
        if (ed_stat == "Bachelor's") {
            default_Payment = default_Payment - (default_Payment * .10);
        }
        if (ed_stat == "Master's") {
            default_Payment = default_Payment - (default_Payment * .20);
        }
        if (mar_stat == "yes") {
            default_Payment = default_Payment - (default_Payment * .20);
        }
        if (emp_stat == "Student") {
            default_Payment = default_Payment + (default_Payment * .10);
        }
        if (emp_stat == "Unemployed") {
            default_Payment = default_Payment - (default_Payment * .05);
        }
        return this.default_Payment;
    }

    public void setAge(int suns) {
        this.age = suns;
    }

    public void setEdu(String edu) {
        this.ed_stat = edu;
    }

    public void setMar(String mar) {
        this.mar_stat = mar;
    }

    public void setEmploy(String employ) {
        this.emp_stat = employ;
    }

    public void setPayment(double payments) {
        this.default_Payment = payments;

    }
    public String toString() {
        return String("Age: " + age() + ", Education : " + ed_stat + ", Marital Status: " + mar_stat + ", Employment Status: " + emp_stat + ", Payment: " + default_Payment);   
    }
}

和InsuranceCalculator.java

import java.util.Scanner;
public class InsuranceCalculator {
    public static void main(String[] args) {
    double insurance = 100;
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter your age: ");
    int years = scan.nextInt();

    System.out.println("Please enter your education level as High School, Bachelor's, or Master's");
    String edu = scan.next();

    System.out.println("Are you married? Yes or No");
    String mar = scan.next();

    System.out.println("Please indicate your employment status as Unemployed, Student, or Employed");
    String employ = scan.next();

    Individual me = new Individual(years, edu, mar, employ, insurance);
    System.out.println(me.toString());

}

请注意,我在Individual.java中有错误,一旦修复Individual.java,我无法告诉Calculator类如何运行。

非常感谢任何帮助,我也将自己工作,并在我解决时更新

1 个答案:

答案 0 :(得分:0)

您希望使用.equals()方法,而不是将字符串与'=='运算符进行比较。例如:

if(ed_stat.equals("Bachelor's")){