gpt4 book ai didi

java - Java 中的数据成员(字段)并打印出来时出现问题?如何正确实现?

转载 作者:行者123 更新时间:2023-12-01 17:40:08 25 4
gpt4 key购买 nike

我想在 printReport() 中打印出 calcWork() 的结果,但我一直得到 0.0 设置 work = this.totalWork; 是否有问题?

public class CarnotEngine {
public double n = 10;
public double R = 4;
public double Th = 8;
public double Tc =2;
public double Va = 4;
public double Vb = 3;
public double totalWork;
public double highTemp, lowTemp;
public double efficientcy;


public CarnotEngine(double highTemp, double lowTemp ){
this.highTemp = highTemp;
this.lowTemp = lowTemp;
}



public double calcWork(){
double Qh;
double work;
Qh = (n*R*highTemp)*log(Vb/Va);
work =(1-lowTemp/highTemp)*Qh;
work = this.totalWork;
return work;


}

public void printReport() {
System.out.println("Simulation Result for Carnot Engine");
System.out.println("work done" + totalWork );

public static void main(String[] args) {
CarnotEngine carengine = new CarnotEngine(500, 200);
carengine.calcWork();
carengine.calcEfficientcy();
carengine.printReport();


}
}

最佳答案

你的作业是倒退的:

work = this.totalWork;

应该是

this.totalWork = work;

现在的方式是计算 work 中的所有内容,并立即用 this.totalWork 中的值覆盖它。

关于java - Java 中的数据成员(字段)并打印出来时出现问题?如何正确实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1890732/

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