gpt4 book ai didi

java - 尝试使用 void 方法中的实例变量 (java)

转载 作者:行者123 更新时间:2023-12-01 18:15:15 26 4
gpt4 key购买 nike

我目前正在编写一个代码,询问包裹尺寸,然后使用体积来计算运费。我还没有在帖子中包含另一个处理成本计算的类。我目前不知道如何获取 inputLength 等方法的输入并将它们放入 Package 和 Package copy 方法中。以及为什么我不能在 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。但我不确定 Shipment 应该如何运作。

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() {
this.length = 1.0; // Removed 'double'
this.width = 1.0;
this.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' needed else shadowing occurs
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;
}
}

关于java - 尝试使用 void 方法中的实例变量 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60382564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com