Java [基本]对象问题

时间:2012-02-01 15:32:34

标签: java java.util.scanner

为模糊的标题道歉,因为我无法想到这个名字是什么。

基本上创建一个计算学生金融支付的小程序。 当我运行它时,它计算对象容差没问题。 然而,无论我尝试对象'助学金',似乎都没有提出0。

代码如下:

import java.util.Scanner;

public class studentFinance implements Payment {
    private String stuname, department, course;
    private float budget, allowance, bursary;
    private int id, currentbudget, attendance;
    private double numbofcourses;

    // student name, id, department, course enrolledon, attendance, bursary,allowance

    //course and numbofcourses already read in
    Scanner in = new Scanner(System.in);

    public studentFinance(float currentBudget) {
        budget = currentbudget;
    }

    public float amendBudget(float newbudget) {
        budget = newbudget;//Transfer data from parameter to instance variable
        return budget;//Return statement
    }

    public float calcPayment() {
        //stuname,department,numbofcourses,attendance,id

        System.out.println("Please enter the student name");
        stuname = in.next();
        System.out.println("Please enter the department name");
        department = in.next();
        System.out.println("Please enter the number of numbofcourses");
        numbofcourses = in.nextDouble();
        System.out.println("Please enter the attendance");
        attendance = in.nextInt();
        System.out.println("Please enter their ID number");
        id = in.nextInt();
        System.out.println("Enter HND,HNC or NC");
        course = in.next();

        if (attendance > 95 & numbofcourses >= 6) {
            allowance = 1000;
        } else if (attendance > 85 & numbofcourses >5) {
            allowance = (1000 * .75F);
        } else if (attendance > 75 & numbofcourses >= 4) {
            allowance = (1000 * .5F);
        } else if (attendance > 75 & numbofcourses >= 3) {
            allowance = 100;
        } else {
            allowance = 0;
        }

        if(course=="HND") {
            bursary = 250;
        } else if(course=="HNC") {
            bursary = 200;
        } else if(course=="NC") {
            bursary = 100;
        } else {
            bursary = 0;
        }

        return bursary + allowance;
    } 

    double payment;

    public void makePayment() {
        System.out.println("The allowance total is : " + payment);
        payment = bursuary + allowance;
    }

    public void print() {

    }


}

如果有人使用这是用于界面的代码,还有其他元素,但我不认为它们与此相关。

interface Payment {
    public float amendBudget(float budget);
    public float calcPayment();
    public void  makePayment();
    public void  print();
}

关心任何帮助。

2 个答案:

答案 0 :(得分:4)

==不符合您的想法。 ==(当应用于对象时)只检查两个对象是否是同一个对象,而不是它们的内容是否相同,因此它不适用于字符串。你想要的是:

if("HND".equals(course)) {
  // ...
} else if ("HNC".equals(course)) {
  // ...
} // etc

一般情况下,您只想在处理基元(==int等)时使用float您明确只想查看两个变量指向完全相同的对象。

你也可以把它们写成:

if(course.equals("HND")) {}

但我更喜欢在常量上使用.equals,因为你知道它们永远不会是null。这可能不是你的问题,但这只是我习惯的模式。

答案 1 :(得分:3)

在比较字符串时,你应该使用.equals而不是==:

    else if(course=="HNC")
    {
      bursary = 200;
    }

    else if(course=="NC")
        {
            bursary = 100;
        }
   else

    {
        bursary = 0;
    }