Java新手,尝试制作基本程序但遇到问题

New to Java, trying to make a basic program but running into problems

本问题已经有最佳答案,请猛点这里访问。

我正在尝试使用用户输入(几乎只是我自己的)和处理非常小的计算来创建一个基本的计算程序。不过,我似乎无法让计算结果返回。我正在这个文件中进行所有计算:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Drug {

private double goalForQuarter = 3716.0;
private double currentScripts;
private double currentDaysIntoQuarter;
private double scriptsNeededDaily100 = goalForQuarter / currentDaysIntoQuarter;
private double scriptsNeededDaily105 = scriptsNeededDaily100 * 1.05;
private double scriptPercentage = currentScripts / scriptsNeededDaily100;


public Drug () {

}

public Drug (double currentScripts) {
    this.currentScripts = currentScripts;
}

public Drug (double currentScripts, double currentDays){
    this.currentScripts = currentScripts;
    this.currentDaysIntoQuarter = currentDays;
}

public double calcDrug100 (){

    return this.scriptPercentage;
}


}

此主程序在此处运行:

导入java.util.scanner;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Main {

    public static void main(String[] args){
        Scanner reader = new Scanner(System.in);

        System.out.print("Input number of days into Quarter:");
        double days = Integer.parseInt(reader.nextLine());
        System.out.println("Input current number of Scripts:");
        double scripts = Integer.parseInt(reader.nextLine());


        Drug drug1 = new Drug(scripts, days);

        System.out.println(drug1.calcDrug100());

    }

}

即使有了用户输入,我还是会打印出0.0。我已经玩过我的变量和方法,但似乎不能使它工作。任何帮助都将不胜感激!


scriptPercentage是一个字段。它不会在currentScriptsscriptsNeededDaily100执行时自动更新。

1
2
3
4
public double calcDrug100 (){
    this.scriptPercentage = this.currentScripts / this.scriptsNeededDaily100;
    return this.scriptPercentage;
}