这是我的主要方法代码:
public class WeightCalculatorTest {
public static void main(String[] args) {
WeightCalculator weCa_init = new WeightCalculator(100);
weCa_init.displayWeight();
WeightCalculator weCa_def = new WeightCalculator();
weCa_def.displayWeight();
WeightCalculator weCa = new WeightCalculator(123);
weCa.setWeight();
weCa.displayWeight();
}
}
方法如下:
import java.util.Scanner;
public class WeightCalculator {
private int weight = 0;
private int hundreds;
private int fifties;
private int twenties;
private int tens;
private int fives;
private int ones;
private int total;
private int[] result;
// Default constructor
public WeightCalculator()
{
weight=0;
System.out.println();
}
// Initial constructor
public WeightCalculator(int w)
{
weight=w;
System.out.println();
}
public void setWeight( ) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Life needs a balance");
System.out.print("How many pounds does the item weight? ");
weight = keyboard.nextInt();
}// end inputWeight
public int[] calculateBalanceWeights() {
hundreds = weight/100;
weight %= 100;
fifties = weight/50;
weight %= 50;
twenties = weight/20;
weight %= 20;
tens = weight/10;
weight %= 10;
fives = weight/5;
ones = weight %5;
total = hundreds+fifties+twenties+tens+fives+ones;
int [] result = {hundreds, fifties, twenties, tens, fives, ones, total};
return result;
}// end calcDays
public void displayWeight() {
System.out.println("You need "+calculateBalanceWeights()[6]+" weights in total:");
System.out.println(" "+hundreds +" 100 lbs");
System.out.println(" "+fifties +" 50 lbs");
System.out.println(" "+twenties +" 20 lbs");
System.out.println(" "+tens +" 10 lbs");
System.out.println(" "+fives +" 5 lbs");
System.out.println(" "+ones +" 1 lbs");
}// end of display
}// end of class Time
我的问题在代码的这一部分:
System.out.println("You need "+calculateBalanceWeights()[6]+" weights in total:");
为什么如果我引用总数不起作用-显示0,而其他变量可以按名称引用。而且,如果我在displayWeight中引用其他varibales作为数百个的calculateBalanceWeights()[0],它不起作用吗?
我真的很困惑其他方法如何存储和引用返回变量?
从主要方法的我也可以打印结果数组
System.out.print(Arrays.toString(weCa.calculateBalanceWeights()));
但是输入的数字结果有误吗?
最佳答案
我没有问题。我猜您的程序运行正常。我运行了您的代码,并得到了以下内容。
对于代码的以下部分:
WeightCalculator weCa_init = new WeightCalculator(100);
weCa_init.displayWeight();
程序输出以下内容。
You need 1 weights in total:
1 100 lbs
0 50 lbs
0 20 lbs
0 10 lbs
0 5 lbs
0 1 lbs
由于
1 100 lbs
,结果包含new WeightCalculator(100)
。对于代码的以下部分:
WeightCalculator weCa_def = new WeightCalculator();
weCa_def.displayWeight();
程序输出以下内容。
You need 0 weights in total:
0 100 lbs
0 50 lbs
0 20 lbs
0 10 lbs
0 5 lbs
0 1 lbs
这次您初始化的对象没有值[
new WeightCalculator()
]。这就是为什么这里的一切都为零。请注意,您有两个构造函数,一个带有参数,一个没有参数。
对于代码的以下部分:
WeightCalculator weCa = new WeightCalculator(123);
weCa.setWeight();
weCa.displayWeight();
程序输出以下内容。
Life needs a balance
How many pounds does the item weight? 373
You need 8 weights in total:
3 100 lbs
1 50 lbs
1 20 lbs
0 10 lbs
0 5 lbs
3 1 lbs
这次您使用值[
new WeightCalculator(123)
]初始化了对象,但是在调用setWeight()
之后,程序将373
作为用户输入并因此提供相应的输出。那么,这里发生的意外事件是什么?
更新资料
为什么我引用
total
不起作用-显示0,而其他变量可以按名称引用。使用时输出为零,
System.out.println("You need "+ total +" weights in total:");
在
displayWeight()
函数中。但是当您编写代码时,代码可以正常工作,
System.out.println("You need " + calculateBalanceWeights()[6] + " weights in total:");
这很明显,因为对函数
calculateBalanceWeights()
的调用会更新所有变量-hundreds, fifties, twenties, tens, fives, ones and total
。当仅在
total
中使用displayWeight()
时,就不会调用calculateBalanceWeights()
函数。结果,没有变量被更新。请执行以下操作以实现所需的行为。
public void displayWeight() {
calculateBalanceWeights();
System.out.println("You need "+total+ " weights in total:");
System.out.println(" "+hundreds +" 100 lbs");
System.out.println(" "+fifties +" 50 lbs");
System.out.println(" "+twenties +" 20 lbs");
System.out.println(" "+tens +" 10 lbs");
System.out.println(" "+fives +" 5 lbs");
System.out.println(" "+ones +" 1 lbs");
}
此代码段正确提供了输出。
如果我将displayWeight中的其他变量引用为
calculateBalanceWeights()[0]
数百个,它将无法正常工作!因为您不能多次调用
calculateBalanceWeights()
。调用calculateBalanceWeights()
后,所有参数值(hundreds, fifties, twenties, tens, fives, ones and total
)都会更新。因此,只需调用一次
calculateBalanceWeights()
并将返回值保存在变量中,然后将其用于打印结果。从主要方法上也可以用
System.out.print(Arrays.toString(weCa.calculateBalanceWeights()));
打印结果数组,但是输入的结果是错误的?数字没有错。我检查了例如,如果用户输入
373
作为输入,System.out.print(Arrays.toString(weCa.calculateBalanceWeights()));
打印以下内容。
[3, 1, 1, 0, 0, 3, 8]
只需将其与您从
calculateBalanceWeights()
返回的内容进行比较即可,如下所示。int [] result = {hundreds, fifties, twenties, tens, fives, ones, total};
希望更新后的答案能解决您的困惑。