从复制构造函数打印用户输入 (java)

Printing user input from a copy constructor (java)

我是 java 的新手,不知道如何使用复制构造函数。请多多包涵。

我正在尝试获取运输包裹的信息。我正在尝试使用复制构造函数重复输入运输详细信息部分。

老实说,我不知道该怎么办。该代码适用于一个包并且没有错误 - 我只需要弄清楚如何提示用户输入第二个包。

import java.util.Scanner;

public class Package {
    
    private static double length = 1.0;
    private static double width = 1.0;
    private static double height = 1.0;
        
    private static double sum1 = length+width+height;
    
        public Package(Package p) {
        this.height = p.height;
        this.length = p.length;
        this.width = p.width;
            }
    
    
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter package dimensions.\nEnter Length: ");
        length = input.nextDouble(); 
    
        System.out.println("\nEnter Width: ");
        width = input.nextDouble();
    
        System.out.println("\nEnter Height: ");
        height = input.nextDouble();

    
    
        System.out.println("Package 1: " + length + " X " + width + " X " + height + ", Volume = " + sum1);
    
    }
    
}

你可以使用 do-while .. 如下所示

Scanner input = new Scanner(System.in);
    int i=0;
do{
        Package package =new Package();
    System.out.println("Enter package dimensions.\nEnter Length: ");
    package.length = input.nextDouble(); 

    System.out.println("\nEnter Width: ");
    package.width = input.nextDouble();

    System.out.println("\nEnter Height: ");
    package.height = input.nextDouble();



    System.out.println("Package : " + package.length + " X " + package.width + " X " + package.height + ", package.Volume = " + sum1);
i++;
}while ( i<=5);

我觉得你的误会是在这一行:

private static double sum1 = length+width+height;

此计算将在运行时执行此行时执行,因此当 class 被加载时(因此长度=1.0,宽度=1.0,高度=1.0)。

如果你想在设置长宽高后计算总和,你必须把计算放在一个方法中:

public static double sum() {
    return length+width+height;
}

并在最后一行调用此方法:

System.out.println("Package 1: " + length + " X " + width + " X " + height + ", Volume = " + sum())

另请注意,您的所有变量都是静态的,因此您并没有真正将包用作对象。为简单起见,您可以删除构造函数,除非您想了解 java 个对象,但您必须调整更多代码。

更新:这可行,但为了能够使用复制构造函数,我每次都必须创建 2 个对象。说到开销。 数学:它不是总和而是体积的积。

import java.util.Scanner;

public class Package {

    double length=1.0;
    double height=1.0;
    double width=1.0;
    double sum;
        
    public Package() {
    }
    
    public Package (Package p)
    {
        this.length = p.length;
        this.height = p.height;
        this.width = p.width;
        // to calculate the volume it's more like a product then a sum
        sum=length*height*width;
    }

    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        for (int i=0;i<5;i++)
        {
            Package pack=new Package();
            
            System.out.println("Enter package dimensions.\nEnter Length: ");
            pack.length = input.nextDouble(); 
        
            System.out.println("\nEnter Width: ");
            pack.width = input.nextDouble();
        
            System.out.println("\nEnter Height: ");
            pack.height = input.nextDouble();
            
            Package p=new Package(pack);
            System.out.println("Package 1: " + p.length + " X " + p.width + " X " + p.height + ", Volume = " + p.sum);
        }
    }
}