【问题标题】:Java: Returning Multiple Values to Main Method In Order To Compute TotalJava:将多个值返回给主要方法以计算总计
【发布时间】:2014-09-25 03:34:28
【问题描述】:

代码如下。该程序根据用户输入的数据运行一系列计算。我的问题是,对于我正在寻找的最重要的东西,总 kg CO2 排放量,我不断得到 0.0 的答案。我需要的是在每种方法中计算的单个总排放量的总和,即使用以下内容打印的值: System.out.println(trans); System.out.println(elec);和 System.out.println(food);

总数应该是 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;

    }

}

【问题讨论】:

  • 您将变量初始化为零,并且永远不会填充正确的值。您需要存储正确的返回值,即 trans = determineTransportationEmission(null);以及其他变量。
  • 啊!!非常感谢!
  • 为什么您的方法采用从未使用过的参数?看起来您应该在 main() 中创建 一个 Scanner 并将其传递给每个方法,而不是在每个方法中创建一个新的 Scanner
  • @Code-Apprentice 我明白了......是的,我现在正在做的事情似乎会导致资源泄漏。我该怎么做呢?
  • 如我所说,在main() 中创建一个 Scanner,然后将其传递给每个方法。您应该了解如何将值传递给方法。

标签: java


【解决方案1】:

啊!!非常感谢Lyju。我做了以下,一切都很好。

从这里:

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

到这里:

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

【讨论】:

    【解决方案2】:

    这里弹出的第二个问题与没有正确地将 Scanner 参数传递给多个方法有关。

    我通过在 main 方法中添加以下内容来解决此问题:

    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);
    }
    

    因为我有三个扫描器对象,每个方法一个,我只是删除了每个扫描器,现在可以将一个扫描器从我的主要方法传递给其他每个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-16
      • 2015-01-18
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多