Java MPG计算器

时间:2014-09-25 16:00:14

标签: java

我已经完成了一个简单的MPG计算器的大部分代码。我现在的主要问题是在程序结束时我需要它来计算用户决定输入的所有里程的平均MPG。

我得到了一些号码,但这不是正确的号码。如果你能够了解我对我的了解,那将是惊人的。如果还有其他问题,请告诉我,我会接受任何批评。

import java.util.Scanner;

public class GasMileage {

    public static void main(String[] args) {
        GasMileage mileage1 = new GasMileage();
        GasMileage mileage2 = new GasMileage();

        Scanner input = new Scanner(System.in);

        System.out.println("Welcome to the MPG calculator!");
        double counterM;
        double counterG;
        double mileage;
        double gallons;
        double mpg;
        double average;

        String repeat = "yes";

        while (repeat.equals("Yes") ||
               repeat.equals("yes") ||
               repeat.equals("y") ||
               repeat.equals("Y")) {

            System.out.println("Enter miles driven: ");
            mileage = input.nextDouble();
            counterM = mileage++;
            mileage1.setMilesDriven(mileage);
            mileage2.setMilesDriven(counterM);

            System.out.println("Enter gallons used: ");
            gallons = input.nextDouble();
            counterG = gallons++;
            mileage1.setGallonsUsed(gallons);
            mileage2.setGallonsUsed(counterG);

            mileage1.setMpg(mileage, gallons);
            mileage2.setMpg(counterM, counterG);
            mpg = mileage1.getMpg();
            System.out.println("Your mpg is: " + mpg);

            System.out.println("repeat? ");
            repeat=input.next();
        }

        average = mileage2.getMpg();
        System.out.println("Your total average mpg is: " + average);
    }

    double milesDriven;
    double gallonsUsed;
    double mpg1;

    public void setMilesDriven(double Miles) {
        milesDriven = milesDriven + Miles;
    }
    public void setGallonsUsed(double Gallons) {
        gallonsUsed = gallonsUsed+Gallons;
    }

    public double getMilesDriven() {
        return milesDriven;
    }

    public double getGallonsUSed() {
        return gallonsUsed;
    }

    public void setMpg(double setM, double setG) {
        mpg1 = (setM) / setG;
    }

    public double getMpg() {
            return mpg1;
    }
}

2 个答案:

答案 0 :(得分:1)

mileage++实际上会增加里程值。

int mileage = 1;
int gallons = mileage++; // mileage will equal 2 after this

作为旁注,永远不要等到清理/格式化代码。清洁代码将更容易阅读,因此更容易发现问题。

答案 1 :(得分:1)

我在代码中发现了一些问题:

  • 请改用repeat.equalsIgnoreCase("YES")
  • 为什么里程++&加仑++,这会改变你的输入。
  • setMpg()未使用对象中已存在的值。
  • mileage1未在循环内重置,因此将更新之前的结果,而不是创建新结果。

如果我没错,您想要为每次旅行和总里程打印里程。所以更新后的代码就像:

import java.util.Scanner;

public class GasMileage {

    public static void main(String[] args) {
        GasMileage mileage2 = new GasMileage();

        Scanner input = new Scanner(System.in);

        System.out.println("Welcome to the MPG calculator!");
        double mileage;
        double gallons;
        double mpg;
        double average;

        String repeat = "yes";

        while (repeat.equalsIgnoreCase("YES")) {
            GasMileage mileage1 = new GasMileage();
            System.out.println("Enter miles driven: ");
            mileage = input.nextDouble();
            mileage1.setMilesDriven(mileage);
            mileage2.setMilesDriven(mileage);

            System.out.println("Enter gallons used: ");
            gallons = input.nextDouble();
            mileage1.setGallonsUsed(gallons);
            mileage2.setGallonsUsed(gallons);

            mpg = mileage1.getMpg();
            System.out.println("Your mpg is: " + mpg);

            System.out.println("repeat? ");
            repeat = input.next();

        }

        average = mileage2.getMpg();
        System.out.println("Your total average mpg is: " + average);
    }

    double milesDriven;
    double gallonsUsed;
    double mpg1;

    public void setMilesDriven(double Miles) {
        milesDriven = milesDriven + Miles;
    }

    public void setGallonsUsed(double Gallons) {
        gallonsUsed = gallonsUsed + Gallons;
    }

    public double getMilesDriven() {
        return milesDriven;
    }

    public double getGallonsUSed() {
        return gallonsUsed;
    }

    public double getMpg() {
        return milesDriven / gallonsUsed;
    }

}

输出:

Welcome to the MPG calculator!
Enter miles driven:  100
Enter gallons used:  25
Your mpg is: 4.0

repeat? yes
Enter miles driven:  200
Enter gallons used:  20
Your mpg is: 10.0
repeat? n

Your total average mpg is: 6.666666666666667 (300 / 45)

如果你想增加里程数,那么在循环外移回里程数1。所以输出就像:

Welcome to the MPG calculator!
Enter miles driven:  100
Enter gallons used:  25
Your mpg is: 4.0

repeat? yes
Enter miles driven:  200
Enter gallons used:  20
Your mpg is: 6.666666666666667
repeat? n

Your total average mpg is: 6.666666666666667 (300 / 45)