有没有更简单的方法来编写这个(java)

时间:2015-02-23 17:28:59

标签: java

class apples{
    public static void main(String args[]){
        int gas = 4;
        int miles = 50000;
        int car1 = 15000;
        int car2 = 30000;
        int mpg1 = 10;
        int mpg2 = 50;
        int gascost1 = (miles / mpg1) * gas;
        int gascost2 = (miles / mpg2) * gas;
        int total1 = (car1 + gascost1);
        int total2 = (car2 + gascost2);
        System.out.println(total1);
        System.out.println(total2);
        if (total1 < total2) 
            System.out.println("Buy car 1");
        else
            System.out.println("Buy car 2");
    }
}

这是一个基本算法,可以显示从长远来看可以省钱的车。如果有人能提供帮助,那就太棒了。 感谢

4 个答案:

答案 0 :(得分:3)

System.out.println(total1 < total2 ? "Buy car 1" : "Buy car 2");

而不是

if (total1 < total2) 
                System.out.println("Buy car 1");
            else
                    System.out.println("Buy car 2");

                }

答案 1 :(得分:0)

OO方式

public class NewClass {

    private int miles = 50000;
    private int gas = 4;

    private class car {

        private int cost;
        private int mpg;

        public int getCost() {
            return cost;
        }

        public void setCost(int cost) {
            this.cost = cost;
        }

        public int getMpg() {
            return mpg;
        }

        public void setMpg(int mpg) {
            this.mpg = mpg;
        }

        @Override
        public String toString() {
            return "car{" + "cost=" + cost + ", mpg=" + mpg + '}';
        }

    }

    public int gasCost(car c) {
        return (miles / c.getMpg()) * gas;

    }

    public int totalCost(car c) {
        return c.getCost() + gasCost(c);
    }

    public void compare(car c1, car c2) {
        if (totalCost(c1) > totalCost(c2)) {
            System.out.println("buy car" + c2);
        }else
        {
              System.out.println("buy car" + c1);
        }
    }

    public car setCar(int Cost, int mpg) {
        car c = new car();
        c.setCost(Cost);
        c.setMpg(mpg);
        return c;
    }

    public static void main(String[] args) {
        NewClass nc = new NewClass();
        car c1=nc.setCar(15000, 10);
        car c2=nc.setCar(30000, 50);
        nc.compare(c1, c2);
    }
}

答案 2 :(得分:0)

The following would be a good approach to follow . It is simple yet flexible in the sense that any number of cars can be created using the car class and any two cars can be compared against accordingly.

class car{
public:
int carCost;
int mpg;
int gasCost;
int totalCost;

int setGasCost(int miles,int gas){

gasCost = (miles / mpg) * gas;
}

int getGasCost(){

return gasCost ;
}

int setTotalCost(int gascost){

totalCost = (carCost + gascost);
}

int getTotalCost(){

return totalCost;
}

}

class apples{
        public static void main(String args[]){    

            int gas = 4;
            int miles = 50000;

            Car car1 = new Car();  
            car1.carCost = 15000;
            car1.mpg = 10;
            car1.setGasCost(miles , gas);
            car1.setTotalCost(car1.getGasCost());

            Car car2 = new Car(); 
            car2.carCost = 30000;            
            car2.mpg = 50;
            car2.setGasCost(miles , gas);
            car2.setTotalCost(car2.getGasCost());

            int totalCar1 = car1.getTotalCost();
            int totalCar2 = car2.getTotalCost();

            if ( totalCar1 < totalCar2 ) 
                System.out.println("Buy car 1");
            else
                    System.out.println("Buy car 2");

                }
            }

答案 3 :(得分:0)

遵循de DRY原则:

public class CarCostTest {
    public static void main(String args[]) {
        int gas = 4;
        int miles = 50000;
        CarCost cc1 = new CarCost(15000, 10);
        CarCost cc2 = new CarCost(30000, 50);
        int total1 = cc1.calcTotalCost(miles, gas);
        int total2 = cc2.calcTotalCost(miles, gas);
        System.out.println(total1 > total2 ? "Car 1 more expensive" : "Car 2 equal or more expensive");
    }

    public static class CarCost {
        private int cost;
        private int mpg;

        public CarCost(int cost, int mpg) {
            this.cost = cost;
            this.mpg = mpg;
        }

        public int calcGasCost(int miles, int gas) {
            return (miles / mpg) * gas;
        }

        public int calcTotalCost(int miles, int gas) {
            return cost + calcGasCost(miles, gas);
        }

    }
}
相关问题