2014-09-25 109 views
0

代碼如下。該程序根據用戶輸入的數據運行一系列計算。我的問題是,對於我所需要的最重要的事情,總二氧化碳排放量,我總是得到0.0的答案。我需要的是在每種方法中計算的單個總排放量的總和,即用以下值打印的值:System.out.println(trans);的System.out.println(ELEC);和System.out.println(食物);Java:將多個值返回主要方法以計算總計

根據用戶提供的輸入值,總數應該是25040之類的東西,但我總是得到總數爲0.0,這顯然是錯誤的。可能與我初始化變量的方式有關,或者與從方法返回值的限制有關。我只是不知道該怎麼做。我應該如何解決這個問題?所有幫助非常感謝!

import java.util.Scanner; 

public class CarbonCalc { 

    public static void main(String[] args) { 
     double trans = 0; 
     double elec = 0; 
     double food = 0; 

     giveIntro(); 

     determineTransportationEmission(null); 
     determineElecticityEmission(null); 
     determineFoodEmission(null); 

     calculateTotalEmission(trans, elec, food); 

     //printReport(trans, elec, food); 
    } 

    //Gives a brief introduction to the user. 
    public static void giveIntro() { 
     System.out.println("This program will estimate your carbon footprint"); 
     System.out.println("(in metric tons per year) by asking you"); 
     System.out.println("to input relevant household data."); 
     System.out.println(""); 
    } 

    //Determines the user's transportation-related carbon emissions. 
    public static double determineTransportationEmission(Scanner input) { 
     Scanner console = new Scanner(System.in); 
     System.out.println("We will first begin with your transportation-related carbon expenditures..."); 
     System.out.print("How many kilometres do you drive per day? "); 
     double kmPerDay = console.nextDouble(); 
     System.out.print("What is your car's fuel efficiency (in km/litre)? "); 
     double fuelEfficiency = console.nextDouble(); 
     System.out.println("We now know that the numeber of litres you use per year is..."); 
     double litresUsedPerYear = 365.00 * (kmPerDay/fuelEfficiency); 
     System.out.println(litresUsedPerYear); 
     System.out.println("...and the kg of transportation-related CO2 you emit must be..."); 

     //Final calculation of transportation-related kgCO2 emissions. 
     double trans = 2.3 * litresUsedPerYear; 
     System.out.println(trans); 
     System.out.println(""); 
     return trans; 
    } 

    //Determines the user's electricity-related carbon emissions. 
    public static double determineElecticityEmission(Scanner input) { 
     Scanner console = new Scanner(System.in); 
     System.out.println("We will now move on to your electricity-related carbon expenditures..."); 
     System.out.print("What is your monthly kilowatt usage (kWh/mo)? "); 
     double kWhPerMonth = console.nextDouble(); 
     System.out.print("How many people live in your home? "); 
     double numPeopleInHome = console.nextDouble(); 
     System.out.println("The kg of electricity-related CO2 you emit must be..."); 

     //Final calculation of electricity-related kgCO2 emissions. 
     double elec = (kWhPerMonth * 12 * 0.257)/numPeopleInHome; 
     System.out.println(elec); 
     System.out.println(""); 
     return elec; 
    } 

    //Determines the user's food-related carbon emissions. 
    public static double determineFoodEmission(Scanner input) { 
     Scanner console = new Scanner(System.in); 
     System.out.println("We will now move on to your food-related carbon expenditures..."); 
     System.out.print("In a given year, what percentage of your diet is meat? "); 
     double meat = console.nextDouble(); 
     System.out.print("In a given year, what percentage of your diet is dairy? "); 
     double dairy = console.nextDouble(); 
     System.out.print("In a given year, what percentage of your diet is fruits and veggies? "); 
     double fruitVeg = console.nextDouble(); 
     System.out.print("In a given year, what percentage of your diet is carbohydrates? "); 
     double carbs = console.nextDouble(); 

     //Final calculation of food-related kgCO2 emissions. 
     System.out.println("The kg of food-related CO2 you emit must be..."); 
     double food = (meat * 53.1 + dairy * 13.8 + fruitVeg * 7.6 + carbs * 3.1); 
     System.out.println(food); 
     System.out.println(""); 
     return food; 
    } 

    //Calculates total emissions across all sources. 
    public static double calculateTotalEmission(double trans, double elec, double food) { 
     System.out.println("Your total kg of CO2 emitted across all sources is equal to..."); 
     double total = trans + elec + food; 
     System.out.println((double) total); 
     System.out.println(""); 
     return total; 

    } 

} 
+2

您將變量初始化爲值零,並且永遠不會填充正確的值。您需要存儲正確的返回值,即trans = determineTransportationEmission(null);和其他變量。 – 2014-09-25 03:42:54

+0

啊!非常感謝你! – santafebound 2014-09-25 04:22:34

+0

爲什麼你的方法需要一個從未使用的參數?看起來你應該在'main()'中創建** 1 **'Scanner',並將它傳遞給每個方法,而不是在每種方法中創建一個新的'Scanner'。 – 2014-09-25 04:28:44

回答

0

啊!非常感謝Lyju。我做了以下,這一切運作良好。

從這:

double trans = 0; 
double elec = 0; 
double food = 0; 

要這樣:

double trans = determineTransportationEmission(null); 
double elec = determineElecticityEmission(null); 
double food = determineFoodEmission(null); 
+0

不用擔心,謝謝 – 2014-09-25 06:18:49

0

其彈出這裏有與不正確地傳遞掃描參數的多種方法做的第二個問題。

我固定,通過加入以下的主要方法:

public static void main(String[] args) { 
    Scanner console = new Scanner(System.in); 

    double trans = determineTransportationEmission(console); 
    double elec = determineElecticityEmission(console); 
    double food = determineFoodEmission(console); 

    giveIntro(); 

    calculateTotalEmission(trans, elec, food); 
} 

而且因爲我有三個掃描器的物體,一個用於每個方法中,我簡單地去除在每個掃描儀和現在可以通過一個單一的掃描儀從我的主要方法到其他每個方法。