Java 为什么可以’;我不能得到一个更好的结果吗?
我是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
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()
方法。