2017-02-15 116 views
0

这里是我的主要方法的代码:引用一个方法的输出在同一个类的另一种方法

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]引用数百,那么它不起作用?

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

ASLO从主要方法我可以打印与

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

但结果是错误的数量进入结果阵列?

+0

也许是一种整数除法的情况,即在方法中使用int变量'1/2 == 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

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

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

如果我参考displayWeight中的其他变量作为calculateBalanceWeights()[0]为几百个它不起作用!

因为您不能多次致电calculateBalanceWeights()。一旦您致电calculateBalanceWeights(),所有参数值(hundreds, fifties, twenties, tens, fives, ones and total)都会更新。

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

Aslo从主要方法我可以打印结果数组与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}; 

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

+0

public void displayWeight(){ System.out。println(“你需要”+ calculateBalanceWeights()[6] +“权重总数:”); System.out.println(“”+数百+“100磅”); System.out.println(“”+五十年代+“50磅”); System.out.println(“”+二十几岁+“20磅”); System.out.println(“”+十+10“磅); System.out.println(“”+ fives +“5磅”); System.out.println(“”+ ones +“1磅”); – Pavel

+0

那里我总计calculateBalanceWeights()[6]参考,而其他变量我引用名称。这是它在我的代码中工作的唯一方式。 – Pavel

+0

感谢您提供非常完整的答案! – Pavel

相关问题