2017-02-15 112 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

相關問題