gpt4 book ai didi

java - 在同一类的另一个方法中引用一个方法的输出

转载 作者:行者123 更新时间:2023-12-02 03:02:25 26 4
gpt4 key购买 nike

这是我的主要方法代码:

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 中的其他变量引用为calculateBalanceWeights()[0] 数百,它不起作用?

我真的很困惑其他方法如何存储和引用返回变量?

Aslo 从主方法中我可以打印结果数组

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

由于new WeightCalculator(100),结果包含1 100 lbs

对于代码的以下部分:

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 为用户输入并相应地提供输出。

那么,这里发生了什么意想不到的事情呢?

<小时/>

更新

Why if I reference total it does not work - shows 0, while other variables can be referenced by name.

使用时输出为零,

System.out.println("You need "+ total +" weights in total:");

displayWeight() 函数内部。

但是当你编写时,你的代码可以正常工作,

System.out.println("You need " + calculateBalanceWeights()[6] + " weights in total:");

这是显而易见的,因为调用函数 calculateBalanceWeights() 会更新所有变量 - 百、五十、二十、十、五、个和总计

当您仅在 displayWeight() 中使用 total 时,您不会调用 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");
}

此代码片段正确提供了输出。

If I reference for other varibales in displayWeight as calculateBalanceWeights()[0] for hundreds it does not work!

因为你不能多次调用calculateBalanceWeights()。调用calculateBalanceWeights()后,所有参数值(百、五十、二十、十、五、个和总计)都会更新。

因此,只需调用一次 calculateBalanceWeights() 并将返回值保存在变量中,然后用它来打印结果。

Aslo from the main method I can print the result array with System.out.print(Arrays.toString(weCa.calculateBalanceWeights())); But the results are wrong for the number entered?

数字没有错。我查了一下。例如,如果用户提供 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};

希望更新的答案能够解决您的困惑。

关于java - 在同一类的另一个方法中引用一个方法的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42262272/

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