如何从另一个方法调用一个双精度浮点数?

huangapple go评论71阅读模式
英文:

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 the area
  • 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中使用它。

尝试以下方法来解决问题:

  1. 在同一个类中定义一个名为area的新字段。
  2. CalculateTotalArea函数中,删除double关键字,并将area更改为this.area
  3. printWords函数中同样地,将area更改为this.area
  4. 不要将函数声明为静态函数,而是使用对象来调用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();
    }
}

其他建议,仅供良好实践:

  1. 为您的Cube类定义一个构造函数。
public class Cube {
    // ...
    
    public Cube(double h, double w, double l) {    
        this.height = h;  
        this.width = w;   
        this.length = l;    
    }
    
    // ...
}
  1. 在创建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:

  1. Define a new field in the same class named area.
  2. In the CalculateTotalArea function, remove the double keyword and make area to this.area.
  3. Replace similarly in printWords function, area to this.area.
  4. 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:

  1. Define a constructor for your Cube class.

    Class(int h, int w, int l){
    this.height = h;
    this.width = w;
    this.length = l;
    }

  2. 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.

huangapple
  • 本文由 发表于 2020年9月26日 12:25:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64073880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定