gpt4 book ai didi

java - Java新手,尝试编写一个基本程序但遇到问题

转载 作者:行者123 更新时间:2023-12-02 03:26:38 24 4
gpt4 key购买 nike

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

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;

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 执行更新时,它不会自动更新。

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

关于java - Java新手,尝试编写一个基本程序但遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38780082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com