这是我的主要方法代码:
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()));
但是输入的数字结果是错误的?
答案 0 :(得分:2)
我没有遇到问题。我想你的程序工作正常。我运行了你的代码,得到了以下内容。
代码的以下部分:
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中的其他varibales引用为
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};
希望更新后的答案会让您感到困惑。