尝试通过void方法(java)使用Instance变量

来源:爱站网时间:2021-12-02编辑:网友分享
我目前正在编写代码,要求包装尺寸,然后使用体积来计算运输成本。我没有在帖子中包含另一个类来处理,对待这个问题,爱站技术小编就去查找了一些资料,终于找到了答案。

问题描述


我目前正在编写代码,要求包装尺寸,然后使用体积来计算运输成本。我还没有在帖子中包含另一个类来处理成本计算。我目前对如何获取inputLength等方法的输入并将其放入Package和Package复制方法一无所知。还有为什么我不能在calcVolume和displayDimensions方法中使用它们。

import java.util.Scanner;

public class Package {

    private double length;
    private double width;
    private double height;
    private Scanner input = new Scanner(System.in);

    public Package() {

        double length = 1.0;
        double width = 1.0;
        double height = 1.0;

    }

    public static void main(String[] args) {

        System.out.printf("Welcome to Colin's Shipping Calculator!%n%n");
        System.out.printf("Enter first package dimensions%n");

        Package volCalc;
        volCalc = new Package();
        volCalc.inputLength();
        volCalc.inputWidth();
        volCalc.inputHeight();

        System.out.printf("Enter second package dimensions%n");

        volCalc.inputLength();
        volCalc.inputWidth();
        volCalc.inputHeight();

        volCalc.displayDimensions();
        volCalc.calcVolume();
        Shipment shipCalc = new Shipment();
        shipCalc.inputPackage();
        shipCalc.inputPackage();
        shipCalc.calculateCost();
        shipCalc.display();



    }

    public Package(double length, double width, double height) {
        this.length = length;
        this.width = width;
        this.height = height;

    }

    public Package(Package copy) {

        Package newPackage = new Package();
        newPackage.length = copy.length;
        newPackage.width = copy.width;
        newPackage.height = copy.height;

    }

    public void inputLength() {
        System.out.printf("Enter Length: ");
        double length = input.nextDouble();


    }

    public void inputWidth() {
        System.out.printf("Enter Width: ");
        double width = input.nextDouble();

    }

    public void inputHeight() {
        System.out.printf("Enter Height: ");
        double height = input.nextDouble();

    }

    public void displayDimensions() {
        System.out.printf(length + " X " + width + " X " + height);

    }

    public double calcVolume() {
        double volume = length*width*height;
        System.out.printf("%nVolume: " + volume);

        return volume;

    }

思路:


您通过在方法中再次声明成员变量来隐藏它们。尝试在函数中的变量名之前删除“ double”。

这里举个例子。我还将每个printf语句更改为print或println。但我不确定装运应如何工作。

import java.util.Scanner;

public class Package {

    private double length;
    private double width;
    private double height;
    private Scanner input = new Scanner(System.in);

    public Package() {
        length = 1.0;           // Removed 'double'
        width = 1.0;            // 'this' needed, else shadowing occurs
        height = 1.0;
    }

    public static void main(String[] args) {
        System.out.println("Welcome to Colin's Shipping Calculator!\n");
        System.out.println("Enter first package dimensions");

        Package packageA;
        packageA = new Package();
        packageA.inputLength();
        packageA.inputWidth();
        packageA.inputHeight();

        packageA.displayDimensions();
        packageA.calcVolume();

        System.out.println("Enter second package dimensions");
        Package packageB = new Package();           // New Package

        packageB.inputLength();
        packageB.inputWidth();
        packageB.inputHeight();

        packageB.displayDimensions();
        packageB.calcVolume();

        Shipment shipCalc = new Shipment();
        shipCalc.inputPackage();
        shipCalc.inputPackage();
        shipCalc.calculateCost();
        shipCalc.display();
    }

    public Package(double length, double width, double height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }

    public Package(Package copy) {
        // Package newPackage = new Package(); Not needed
        this.length = copy.length;  // 'this' just for clarification.
        this.width = copy.width;
        this.height = copy.height;
    }

    public void inputLength() {
        System.out.print("Enter Length: ");
        length = input.nextDouble();    // Removed 'double'
    }

    public void inputWidth() {
        System.out.print("Enter Width: ");
        width = input.nextDouble();     // Removed 'double'
    }

    public void inputHeight() {
        System.out.print("Enter Height: ");
        height = input.nextDouble();    // Removed 'double'
    }

    public void displayDimensions() {
        System.out.println(length + " X " + width + " X " + height);
    }

    public double calcVolume() {
        double volume = length * width * height;
        System.out.println("Volume: " + volume);

        return volume;
    }
}

 以上内容就是爱站技术频道小编为大家分享的尝试通过void方法(java)使用Instance变量,看完以上分享之后,大家应该都知道怎么操作了吧。

上一篇:为什么ArrayList.add()将值定义为a = b时将值添加到Java中的多个arraylist?

下一篇:怎么将列表放入地图中

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载