【问题标题】:Referencing one method's output in another method of same class在同一类的另一个方法中引用一个方法的输出
【发布时间】:2017-02-15 23:48:08
【问题描述】:

这是我的主要方法代码:

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] 数百个它不起作用?

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

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

System.out.print(Arrays.toString(weCa.calculateBalanceWeights()));

但是输入的数字结果有误?

【问题讨论】:

  • 可能是整数除法的情况,即使用 int 变量 1 / 2 == 0

标签: java class variables


【解决方案1】:

我没有发现问题。我猜你的程序运行正常。我运行了你的代码并得到了以下结果。

对于代码的以下部分:

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

当您在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");
}

此代码 sn-p 正确提供输出。

如果我将 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};

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

【讨论】:

  • 方法中 public void displayWeight() { System.out.println("你需要 "+calculateBalanceWeights()[6]+" 权重:"); System.out.println(" "+hundreds +" 100 lbs"); System.out.println(""+50+"50 磅"); System.out.println(" "+twenties +" 20 lbs"); System.out.println(" "+十 +" 10 磅"); System.out.println(" "+fives +" 5 lbs"); System.out.println(" "+ones +" 1 lbs");
  • 我参考了总的 calculateBalanceWeights()[6],而其他变量我按名称参考。这是它在我的代码中工作的唯一方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-07
  • 2011-01-14
  • 2018-09-15
  • 2013-10-08
相关资源
最近更新 更多