英文:
How to call a double from another method?
问题
public class Cube {
public double height;
public double width;
public double length;
public double area;
static void CalculateTotalArea(double height, double width, double length) {
double area = height * width * length;
}
public void printWords() {
System.out.println("This cube has a height of " + height +
", a width of " + width +
", and a depth of " + length +
". Its total area is: " + area);
}
}
import java.util.Scanner;
public class Lab5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please type the Height, Width, and Length" +
" of the cube you would like to see the area of." +
" For example 10 5 2");
// allows "scan(Hight, Width, and Length)" to be given a typed number
double scanHeight = scanner.nextDouble();
double scanWidth = scanner.nextDouble();
double scanLength = scanner.nextDouble();
Cube finalCube = new Cube();
finalCube.height = scanHeight;
finalCube.width = scanWidth;
finalCube.length = scanLength;
finalCube.CalculateTotalArea(finalCube.height, finalCube.width, finalCube.length);
finalCube.printWords();
}
}
英文:
I'm new to coding and I'm having trouble with trying to get a double from another method. In trying to make it so I can set the length, width, and height of a cube and it outputs these numbers and the area, but I can't get one of my methods to take the math problem I made?
Cube.java
public class Cube {
public double height;
public double width;
public double length;
static void CalculateTotalArea(double height, double width, double length) {
double area = (double) (height * width * length);
}
public void printWords(){
System.out.println("This cube has a height of " + height +
" , a width of " + width +
" , and a depth of " + length +
". Its total area is: " + area);
}
}
What I'm trying to do in printWords
, is have it print the numbers type and show the area but it won't take my area from the math problem above it.
I don't know if you need to see the other class to understand what I'm doing so I'm just gonna post that too.
Lab5.java
import java.util.Scanner;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please type the Height, Width, and Length"
+ " of the cube you would like to see the area of."
+ " For example 10 5 2");
// allows "scan(Hight, Width, and Length)" to be given a typed number
double scanHeight = scanner.nextInt();
double scanWidth = scanner.nextInt();
double scanLength = scanner.nextInt();
Cube finalCube = new Cube();
finalCube.height = scanHeight;
finalCube.width = scanWidth;
finalCube.length = scanLength;
Cube.CalculateTotalArea(finalCube.height, finalCube.width, finalCube.length);
finalCube.printWords();
}
答案1
得分: 0
你在这里犯了几个错误:
- 你的代码不应该能够编译通过。在你的
printWords()
方法中,你没有定义area
。 - 你将计算出的面积赋值给了一个局部变量。因此,你的打印方法无法获取到那个值。
将你的 Cube.java
修改成这样:
public class Cube {
public double height;
public double width;
public double length;
public double area;
public void calculateTotalArea() {
this.area = this.height * this.width * this.length;
}
public void printWords() {
System.out.println("这个立方体的高度为 " + height +
",宽度为 " + width +
",深度为 " + length +
"。它的总面积是:" + area);
}
}
然后通过实例调用计算方法:
finalCube.calculateTotalArea();
finalCube.printWords();
英文:
You are making several mistakes here
- Your code should not compile. In your
printWords()
you didn't define thearea
- You are assigning the calculated area to a local variable. So your print method does have scope to get that.
Change your Cube.java
to this
public class Cube {
public double height;
public double width;
public double length;
public double area;
public void calculateTotalArea() {
this.area = this.height * this.width * this.length;
}
public void printWords() {
System.out.println("This cube has a height of " + height +
" , a width of " + width +
" , and a depth of " + length +
". Its total area is: " + area);
}
}
And call the calculate method by instance
finalCube.calculateTotalArea();
finalCube.printWords();
答案2
得分: 0
因此,在Cube
类中没有将计算得到的面积存储在任何字段中,因此当您调用printWords
函数时,此时面积中的值未定义。
area
变量的作用域限于CalculateTotalArea
函数。您不能在printWords
中使用它。
尝试以下方法来解决问题:
- 在同一个类中定义一个名为
area
的新字段。 - 在
CalculateTotalArea
函数中,删除double
关键字,并将area
更改为this.area
。 - 在
printWords
函数中同样地,将area
更改为this.area
。 - 不要将函数声明为静态函数,而是使用对象来调用
printWords
函数。
finalCube.CalculateTotalArea(finalCube.height, finalCube.width, finalCube.length);
因此,在最终更改之后,代码如下:
public class Cube {
public double height;
public double width;
public double length;
public double area;
public void CalculateTotalArea(double height, double width, double length) {
this.area = (double) (height * width * length);
}
public void printWords() {
System.out.println("This cube has a height of " + height +
", a width of " + width +
", and a depth of " + length +
". Its total area is: " + this.area);
}
}
将您的Lab5.java更改为:
import java.util.Scanner;
public class Lab5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please type the Height, Width, and Length"
+ " of the cube you would like to see the area of."
+ " For example 10 5 2");
double scanHeight = scanner.nextDouble();
double scanWidth = scanner.nextDouble();
double scanLength = scanner.nextDouble();
Cube finalCube = new Cube();
finalCube.height = scanHeight;
finalCube.width = scanWidth;
finalCube.length = scanLength;
finalCube.CalculateTotalArea(finalCube.height, finalCube.width, finalCube.length);
finalCube.printWords();
}
}
其他建议,仅供良好实践:
- 为您的
Cube
类定义一个构造函数。
public class Cube {
// ...
public Cube(double h, double w, double l) {
this.height = h;
this.width = w;
this.length = l;
}
// ...
}
- 在创建
Cube
实例时,使用此构造函数。
Cube finalCube = new Cube(scanHeight, scanWidth, scanLength);
英文:
Since, you are not storing the area calculated in any field in Cube
class so when you are calling the printWords
function, at that time, the value in the area is not defined.
The scope of the area
variable is limited to CalculateTotalArea
function. You can't use it in printWords
.
Try this to solve the problem:
- Define a new field in the same class named
area
. - In the
CalculateTotalArea
function, remove thedouble
keyword and make area tothis.area
. - Replace similarly in
printWords
function,area
tothis.area
. - Instead of calling as static function, use the object to call the
printWords
function.
finalCube.CalculateTotalArea(finalCube.height, finalCube.width, finalCube.length);
So, after the final changes, it looks like this:
public class Cube {
public double height;
public double width;
public double length;
public double area;
static void CalculateTotalArea(double height, double width, double length) {
this.area = (double) (height * width * length);
}
public void printWords(){
System.out.println("This cube has a height of " + height +
" , a width of " + width +
" , and a depth of " + length +
". Its total area is: " + this.area);
}
}
And change your Lab5.java to this:
import java.util.Scanner;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please type the Height, Width, and Length"
+ " of the cube you would like to see the area of."
+ " For example 10 5 2");
// allows "scan(Hight, Width, and Length)" to be given a typed number
double scanHeight = scanner.nextInt();
double scanWidth = scanner.nextInt();
double scanLength = scanner.nextInt();
Cube finalCube = new Cube();
finalCube.height = scanHeight;
finalCube.width = scanWidth;
finalCube.length = scanLength;
finalCube.CalculateTotalArea(finalCube.height, finalCube.width, finalCube.length);
finalCube.printWords();
}
Other suggestions, just for good practices:
-
Define a constructor for your
Cube
class.Class(int h, int w, int l){
this.height = h;
this.width = w;
this.length = l;
} -
While making a new instance of
Cube
, using this constructor.
Cube finalCube = new Cube(scanHeight, scanWidth, scanLength);
答案3
得分: 0
基本上,您在Cube
类中没有声明一个实例变量area
。这就是为什么您会遇到错误。
我已经将代码更新为以下内容:
public class Cube {
public double height;
public double width;
public double length;
public double calculateTotalArea(double height, double width, double length) {
return (height * width * length);
}
public void printWords() {
System.out.println("This cube has a height of " + height + " , a width of " + width + " , and a depth of "
+ length + ". Its total area is: " + calculateTotalArea(height, width, length));
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please type the Height, Width, and Length"
+ " of the cube you would like to see the area of." + " For example 10 5 2");
// allows "scan(Hight, Width, and Length)" to be given a typed number
double scanHeight = scanner.nextDouble();
double scanWidth = scanner.nextDouble();
double scanLength = scanner.nextDouble();
Cube finalCube = new Cube();
finalCube.height = scanHeight;
finalCube.width = scanWidth;
finalCube.length = scanLength;
finalCube.printWords();
}
}
就是这些。
英文:
Basically, you didn't declare an instance variable area
in the Cube
class. That's why you are facing error.
I have updated the code to this :
public class Cube {
public double height;
public double width;
public double length;
public double calculateTotalArea(double height, double width, double length) {
return (height * width * length);
}
public void printWords() {
System.out.println("This cube has a height of " + height + " , a width of " + width + " , and a depth of "
+ length + ". Its total area is: " + calculateTotalArea(height, width, length));
}
}
Update main method to this :
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please type the Height, Width, and Length"
+ " of the cube you would like to see the area of." + " For example 10 5 2");
// allows "scan(Hight, Width, and Length)" to be given a typed number
double scanHeight = scanner.nextInt();
double scanWidth = scanner.nextInt();
double scanLength = scanner.nextInt();
Cube finalCube = new Cube();
finalCube.height = scanHeight;
finalCube.width = scanWidth;
finalCube.length = scanLength;
finalCube.printWords();
}
That's all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论