class AlgosPart2
{
private double f2c;
private double farenheit;
public void f2c(double farenheit)
{
double celcius = (farenheit - 32) * 5/9;
System.out.printf("F2C: %.2f%n", celcius);
this.f2c = f2c;
}
public double getf2c()
{
return f2c;
}
}我在弄清楚把法伦海特转换成大提琴的公式放在哪里有点儿麻烦。我把它放错地方了吗?因为当我调用它的时候,驱动程序仍然没有检测到这个方法。司机:
public class Driver {
public static void main(String[] args)
{
AlgosPart2 ap2 = new AlgosPart2();
double x1 = getf2c(20);
}
}20被认为是代表法伦海特,但错误发生在这里。不太确定我哪里出了问题。有什么想法吗?
发布于 2018-11-17 06:33:56
我怀疑您想要用一种方法f2c(...)将华氏转换为摄氏,然后使用f2c的getter。将代码更改如下:
class AlgosPart2 {
private double f2c;
public void f2c(double farenheit) {
this.f2c = (farenheit - 32) * 5 / 9;
}
public double getf2c() {
return f2c;
}
}然后按如下方式命名:
public static void main(String[] args) {
AlgosPart2 ap2 = new AlgosPart2();
ap2.f2c(20);
System.out.println(ap2.getf2c());
}在这里,我们使用对象f2c调用方法ap2,然后使用getter可以访问以前分配的f2c值。
发布于 2018-11-17 06:27:52
就像下面这样
AlgosPart2 ap2 = new AlgosPart2();
double x1 = ap2.f2c(20);像这样改变这个方法
public double f2c(double farenheit)
{
double celcius = (farenheit - 32) * 5/9;
System.out.printf("F2C: %.2f%n", celcius);
return celcius;
}https://stackoverflow.com/questions/53348811
复制相似问题