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

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

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);
    }
}
英文:

I am new to java and cannot figure out how to work the copy constructors. Please bear with me.

I am trying to get information for shipping packages. I am trying to use the copy constructor to repeat the enter shipping details section.

I honestly have no idea what to do with it. The code works fine for one package and there are no errors - I just need to figure out how to prompt a user for a second package.

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);
	
	}
	
}

答案1

得分: 0

你可以使用do-while来实现这个... 类似下面这样:

Scanner input = new Scanner(System.in);
int i = 0;
do {
    Package package = new Package();
    System.out.println("请输入包裹尺寸。\n输入长度:");
    package.length = input.nextDouble();

    System.out.println("\n输入宽度:");
    package.width = input.nextDouble();

    System.out.println("\n输入高度:");
    package.height = input.nextDouble();

    System.out.println("包裹: " + package.length + " X " + package.width + " X " + package.height + ", 包裹体积 = " + sum1);
    i++;
} while (i <= 5);
英文:

You can use do-while for this .. something like below

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

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

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



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

答案2

得分: 0

我认为你的误解在于这行代码:

private static double sum1 = length+width+height;

这个计算将在运行时执行此行代码的时候进行,也就是在类加载时(所以length=1.0width=1.0height=1.0)。

如果你希望在设置了lengthwidthheight 之后再计算总和,你需要将计算放在一个方法中:

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

然后在最后一行调用这个方法:

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

还要注意,你所有的变量都是静态的,所以你实际上没有将 Package 作为对象使用。为了简单起见,你可以移除构造函数,除非你想学习关于 Java 对象,但那样你需要适应更多的代码。

更新:这样可以工作,但为了能够使用拷贝构造函数,我每次都必须创建两个对象。说到开销的问题,不是求和而是体积的乘积。

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);
        }
    }
}
英文:

I think your misunderstanding is in this line:

private static double sum1 = length+width+height;

This computation will be executed at the moment the runtime executes this line so when the class is loaded (so length=1.0, width=1.0, height=1.0).

If you want the sum to be calculated after you have set the length, width and height, you have to put the calculation in a method:

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

And in your last line call this method:

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

Also notice that all your variables are static so you don't really use the Package as an object. For simplicity you can remove the constructor except if you want to learn about java objects but then you have to adapt more code.

UPDATE: this works but to be able to use the copy constructor I each time have to create 2 objects. Speaking of overhead.
Mathimatics: it is not sum but product for volumes.

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&#39;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&lt;5;i++)
{
Package pack=new Package();
System.out.println(&quot;Enter package dimensions.\nEnter Length: &quot;);
pack.length = input.nextDouble(); 
System.out.println(&quot;\nEnter Width: &quot;);
pack.width = input.nextDouble();
System.out.println(&quot;\nEnter Height: &quot;);
pack.height = input.nextDouble();
Package p=new Package(pack);
System.out.println(&quot;Package 1: &quot; + p.length + &quot; X &quot; + p.width + &quot; X &quot; + p.height + &quot;, Volume = &quot; + p.sum);
}
}
}

huangapple
  • 本文由 发表于 2020年10月14日 22:01:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64354992.html
匿名

发表评论

匿名网友

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

确定