Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java[Basic]对象问题_Java_Java.util.scanner - Fatal编程技术网

Java[Basic]对象问题

Java[Basic]对象问题,java,java.util.scanner,Java,Java.util.scanner,很抱歉标题含糊不清,因为我想不出名字是什么 基本上是创建一个计算学生财务付款的小程序。 当我运行它时,它会计算对象余量,这没问题。 然而,无论我尝试什么,除了0之外,“助学金”这个对象似乎什么都没有 代码如下: import java.util.Scanner; public class studentFinance implements Payment { private String stuname, department, course; private float bu

很抱歉标题含糊不清,因为我想不出名字是什么

基本上是创建一个计算学生财务付款的小程序。 当我运行它时,它会计算对象余量,这没问题。 然而,无论我尝试什么,除了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();
}

感谢您的帮助。

在比较此处的字符串时,您应该使用.equals而不是==:

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

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

    {
        bursary = 0;
    }

==
不会做你认为它在这里做的事情<代码>=(应用于对象时)只是检查两个对象是否是同一个对象,而不是它们的内容是否相等,因此这里不适用于字符串。你想要的是:

if("HND".equals(course)) {
  // ...
} else if ("HNC".equals(course)) {
  // ...
} // etc
通常,在处理原语(
int
float
等)时,您只想使用
==
,或者您明确地只想查看两个变量是否指向完全相同的对象

您也可以将它们写为:

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

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

为此干杯,我身边的一个非常有趣的人在同一时间就发现了。这甚至可以编译吗?你引用了一个变量“bursuary”,我想它应该引用“bursary”,这确实是一个很好的观点,在编译时我注意到了这一点。此外,类名通常应该是CamelCase,因此您的类可能应该称为StudentFinance。变量通常是camelCase(第一个字符较低),您有时使用它,但不是一直使用它。这不会改变代码的行为,但符合Java风格的标准会使大多数已经熟悉这些标准的人更容易阅读。@HertenFerford Plz如果您对以下答案满意,请将答案标记为已接受的答案。