gpt4 book ai didi

java - 无法调用方法,试图将变量从一个方法获取到另一个方法

转载 作者:行者123 更新时间:2023-12-02 01:05:23 26 4
gpt4 key购买 nike

我在尝试调用方法时陷入困境。无论我做什么,我都会收到一条错误消息,指出找不到该符号。我正在尝试使用变量total(来自surfaceArea方法)和volume(来自volume方法)。

问题出在 toString 方法中,无论我做什么它都看不到变量。我确信这是我所缺少的非常基本的东西,所以我希望有人能弄清楚。

这是错误日志:

Ellipsoid.java:176: error: cannot find symbol
String vout = df.format(volume);
^
symbol: variable volume
location: class Ellipsoid
Ellipsoid.java:177: error: cannot find symbol
String sout = df.format(total);
^
symbol: variable total
location: class Ellipsoid
2 errors

这是代码本身。我试图让它尽可能容易阅读:


/**
* This program lets the user enter values of and Ellipsoid.
* @version 02/05/2020
*/
public class Ellipsoid {

// fields

private String label;
private double a, b, c;
//public double total, volume;
// constructor
/**
* This constructor creates a ellipsoid and gets its information.
*
* @param labelIn is the label entered by the user.
* @param aIn is the a valuve entered by the user.
* @param bIn is the b valuve entered by the user.
* @param cIn is the c valuve entered by the user.
*/
public Ellipsoid(String labelIn, double aIn, double bIn, double cIn) {
setLabel(labelIn);
setA(aIn);
setB(bIn);
setC(cIn);
}
// methods
/**
* This method gets the label string.
* @return returns the label of the ellipsoid.
*/
public String getLabel() {
return label;
}
/**
* This method sets the label of the ellipsoid.
* @param labelIn is the label entered by the user.
* @return returns true or false depending on user input.
*/
public boolean setLabel(String labelIn) {
if (labelIn == null) {
return false;
}
else {
label = labelIn.trim();
return true;
}
}
/**
* This method gets the a values of the ellipsoid.
* @return returns a values of the ellipsoid.
*/
public double getA() {
return a;
}
/**
* This method sets the a value of the ellipsoid.
* @param aIn is the a value entered by the user.
* @return returns true or false depending on the user input.
*/
public boolean setA(double aIn)
{
if (aIn > 0)
{
a = aIn;
return true;
}
else
{
return false;
}
}
/**
* This method gets the b value of the ellipsoid.
* @return returns the b value of the ellipsoid.
*/
public double getB()
{
return b;
}
/**
* This method sets the b value of the ellipsoid.
* @param bIn is the b value entered by the user.
* @return returns true or false depending on the user input.
*/
public boolean setB(double bIn)
{
if (bIn > 0)
{
b = bIn;
return true;
}
else
{
return false;
}
}

/**
* This method gets the c value of the ellipsoid.
* @return returns the c value of the ellipsoid.
*/

public double getC()
{
return c;
}
/**
* This method sets the c value of the ellipsoid.
* @param cIn is the c value entered by the user.
* @return returns true or false depending on the user input.
*/
public boolean setC(double cIn)
{
if (cIn > 0)
{
c = cIn;
return true;
}
else
{
return false;
}
}

/**
* This method finds the volume of the ellipsoid.
* @return returns the volume of the ellipsoid.

*/
public double volume()
{
double volume = 4 * Math.PI * a * b * c;
volume = volume / 3;
return volume;
}

/**
* This method finds the surface area of the ellipsoid.
* @return returns the surface area.
*/
public double surfaceArea() {

double ab = (a * b);
ab = Math.pow(ab, 1.6);
double ac = a * c;
ac = Math.pow(ac, 1.6);
double bc = b * c;
bc = Math.pow(bc, 1.6);

double top = ab + ac + bc;
double bottom = top / 3;
double full = bottom * 1 / 1.6;
double total = 4 * Math.PI * full;
return total;

}



/**
* This method prints the information of the ellipsoid.
* @return returns the information of the ellipsoid.
*/
public String toString() {
DecimalFormat df = new DecimalFormat("#,##0.0###");
surfaceArea();
volume();

String aout = df.format(a);
String bout = df.format(b);
String cout = df.format(c);
String vout = df.format(volume);
String sout = df.format(total);


String output = "Ellipsoid \"" + label + "\" with axes a = " + aout
+ ", b = " + bout + ", c = " + cout + " units has:\n\tvolume = "
+ vout + " cubic units\n\tsurface area = "
+ sout + " square units";
return output;
}

}

最佳答案

volumetotal 分别是 volume()surfaceArea() 方法的本地成员。它在 toString() 方法中不可见。但 a、b 和 c 是可见的,因为它们被声明为类级别。尝试将这些方法的返回值分配给 toString() 中的局部变量,如下所示:

public String toString() {
DecimalFormat df = new DecimalFormat("#,##0.0###");
double total = surfaceArea();
double volume = volume();

....
String vout = df.format(volume);
String sout = df.format(total);
....
}

关于java - 无法调用方法,试图将变量从一个方法获取到另一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60123352/

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