Java 为什么可以’;我不能得到一个更好的结果吗?

Java 为什么可以’;我不能得到一个更好的结果吗?,java,Java,我是java新手,我的编译器没有告诉我这段代码的错误是什么: public class Main { public static void main(String args[]) { class gasStation { double price; double distance; } gasStation shell = new gasStation(); shell.p

我是java新手,我的编译器没有告诉我这段代码的错误是什么:

public class Main {

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

            double price;
            double distance;
        }
        gasStation shell = new gasStation();
        shell.price = 2.72;
        shell.distance = 1.25;
        gasStation exxon = new gasStation();
        exxon.price = 2.35;
        exxon.distance = 1.75;
        class betterDeal {

            public gasStation compare(gasStation shell, gasStation exxon) {
                double shellRating = shell.price * shell.distance;
                double exxonRating = exxon.price * exxon.distance;
                if (shellRating > exxonRating) {
                    gasStation better = shell;
                } else if (shellRating < exxonRating) {
                    gasStation better = exxon;
                }
                return better;
            }

            System.out.println (better);
        }
    }

}
公共类主{
公共静态void main(字符串参数[]){
等级加气站{
双倍价格;
双倍距离;
}
气体站外壳=新气体站();
shell.price=2.72;
外壳距离=1.25;
加气站埃克森=新加气站();
埃克森美孚股价=2.35;
埃克森美孚距离=1.75;
班主任{
公共加油站比较(加油站壳牌、加油站埃克森){
双shellRating=shell.price*shell.distance;
双重埃克森评级=埃克森价格*埃克森距离;
if(壳牌评级>埃克森美孚评级){
气体含量=外壳;
}否则如果(壳牌评级<埃克森美孚评级){
加气更好=埃克森美孚;
}
回报更好;
}
System.out.println(更好);
}
}
}

您的代码中有几个错误

  • if
    范围外初始化变量
    更好

    gasStation better = null;
    if (shellRating > exxonRating) {
        better = shell;
    } else if (shellRating < exxonRating) {
        better = exxon;
    }
    return better;
    
  • 您可以从
    Main
    类中获取您的
    gastation
    类和
    betterDeal

  • class gasStation {
    
        double price;
        double distance;
    
        public String toString() {
            return "Price: " + price + "\nDistance: " + distance;
        }
    }
    
  • 为什么我不能在代码末尾打印对象“更好”?这是因为您从未调用
    compare
    方法。创建
    betterDeal
    类的新对象,并在
    main
    方法中调用
    compare
    方法来打印变量
    better

    new betterDeal().compare(shell, exxon);
    
  • 但是,如果仍然需要打印
    better
    变量的
    price
    distance
    ,则必须重写
    gastation
    类中的
    toString()
    方法

    class gasStation {
    
        double price;
        double distance;
    
        public String toString() {
            return "Price: " + price + "\nDistance: " + distance;
        }
    }
    

完整代码:

public class Main {

    public static void main(String args[]) {
        gasStation shell = new gasStation();
        shell.price = 2.72;
        shell.distance = 1.25;
        gasStation exxon = new gasStation();
        exxon.price = 2.35;
        exxon.distance = 1.75;

        new betterDeal().compare(shell, exxon);
    }

}

class gasStation {

    double price;
    double distance;

    public String toString() {
        return "Price: " + price + "\nDistance: " + distance;
    }
}

class betterDeal {

    public gasStation compare(gasStation shell, gasStation exxon) {
        double shellRating = shell.price * shell.distance;
        double exxonRating = exxon.price * exxon.distance;

        gasStation better = null;
        if (shellRating > exxonRating) {
            better = shell;
        } else if (shellRating < exxonRating) {
            better = exxon;
        }
        System.out.println(better);
        return better;
    }

}
公共类主{
公共静态void main(字符串参数[]){
气体站外壳=新气体站();
shell.price=2.72;
外壳距离=1.25;
加气站埃克森=新加气站();
埃克森美孚股价=2.35;
埃克森美孚距离=1.75;
新betterDeal().比较(壳牌、埃克森);
}
}
等级加气站{
双倍价格;
双倍距离;
公共字符串toString(){
返回“价格:”+Price+“\n状态:”+distance;
}
}
班主任{
公共加油站比较(加油站壳牌、加油站埃克森){
双shellRating=shell.price*shell.distance;
双重埃克森评级=埃克森价格*埃克森距离;
气体浓度=零;
if(壳牌评级>埃克森美孚评级){
更好=外壳;
}否则如果(壳牌评级<埃克森美孚评级){
更好=埃克森美孚;
}
System.out.println(更好);
回报更好;
}
}

请格式化您的代码。还有你的问题是什么<代码>为什么我不能得到一个更好的gastation结果?
对我来说没有任何意义,因为你试图将类放在main中。这是不正确的。如果我是你,我会参考javadoc教程:你的返回更好,但不调用比较方法并存储返回结果为什么我不能在代码末尾打印对象“更好”。对不起,我是一个完蛋的人newb@user10635007正如我所说的,你正试图把所有的东西都放在你的主框架内。您需要单独创建类,然后在main中构造对象并调用object上的方法。我已经更新了我的答案。观察错误。检查完整代码。@user10635007它打印更好的价格和距离。根据您的意愿更改
toString()
方法。