Java 我不知道为什么getCost();正在返回一个错误
我目前正在为学校做一个小项目,我不知道为什么这会引起问题Java 我不知道为什么getCost();正在返回一个错误,java,Java,我目前正在为学校做一个小项目,我不知道为什么这会引起问题 package tripcalculator; import java.util.Scanner; public class Trip { Scanner kbd = new Scanner(System.in); private int distance; public final double MILEAGE = 0.14; public final double COST_PER_LITRE =
package tripcalculator;
import java.util.Scanner;
public class Trip {
Scanner kbd = new Scanner(System.in);
private int distance;
public final double MILEAGE = 0.14;
public final double COST_PER_LITRE = 1.29;
public void getLitresUsed() {
}
//constructor with distance parameter passed
public Trip(int distance) {
}
//default construtor
public Trip() {
System.out.println("Enter distance travelled: ");
distance = kbd.nextInt();
}
//getter and setter
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public double getLitresUsed(int distance){
double litresUsed = MILEAGE * distance;
return litresUsed;
}
public double getCost(double litresUsed){
double cost = litresUsed * COST_PER_LITRE;
return cost;
}
public String toString(String litresUsed) {
getLitresUsed();
getCost();
String output = "Trip Details\n" + "\n" + "Distance: " + distance + " km\n" + "\n" + "Litres Used: " + litresUsed + "\n" + "\n" + "Cost: $" + cost;
return output;
}
}
正如您所看到的,getCost存在问题,我不确定为什么会出现问题。我的主要代码如下:
package tripcalculator;
public class TripCalculator {
public static void main(String[] args) {
Trip trip1 = new Trip();
trip1.getLitresUsed();
showTrip(trip1);
}
public static void showTrip(Trip trip1) {
System.out.println(trip1.toString());
}
}
这是一个用于计算旅行总成本的程序,我不知道为什么它没有从getCost返回值。如果有人能解释这一点,我将不胜感激 杰克
我已经重构了你的程序。仔细阅读并分析我的评论
Trip.java:
package tripcalculator;
import java.util.Scanner;
public class Trip {
Scanner kbd = new Scanner(System.in);
private double distance; // This should be a double value
final double MILEAGE = 0.14; // Let;s make constants private also. They are not needed for the world.
final double COST_PER_LITRE = 1.29;
// public void getLitresUsed() { // What was the purpose of this? Removed.
//
// }
// constructor with distance parameter passed
// public Trip(int distance) { // Actually, this constructor does not do
// anything. Removed.
// }
// default constructor
public Trip() {
System.out.println("Enter distance travelled: ");
distance = kbd.nextInt();
}
// getter and setter
public double getDistance() {
return distance;
}
// public void setDistance(int distance) { // We do not need setter method as
// distance is being set when constructing Trip object.
// this.distance = distance;
// }
public double getLitresUsed() { // distance is class variable. No need in method parameter distance. Removed.
double litresUsed = MILEAGE * distance;
return litresUsed;
}
public double getCost() { // litresUsed is being calculated by method. So, parameters has been removed and
// changed to method output.
double cost = getLitresUsed() * COST_PER_LITRE;
return cost;
}
@Override // Actually, we're overriding method here. Be careful.
public String toString() {
// getLitresUsed(); // These methods' outputs have been moved to String
// generation.
// getCost();
String output = "Trip Details\n" + "\n" + "Distance: " + distance + " km\n" + "\n" + "Litres Used: "
+ getLitresUsed() + "\n" + "\n" + "Cost: $" + getCost();
return output;
}
}
TripCalculator.java:
package tripcalculator;
public class TripCalculator {
public static void main(String[] args) {
Trip trip1 = new Trip();
// trip1.getLitresUsed(); // No need in calling this method from outside.
showTrip(trip1);
}
public static void showTrip(Trip trip1) {
System.out.println(trip1.toString());
}
}
祝你好运 有什么问题吗?您没有指定错误。对不起,我忘记指定这样的@Rab,getCost();正在请求输入,我不确定您为什么能告诉我错误?我没有打开IDE。请研究错误/异常的含义。查看javadoc中的异常,例如,无法将类Trip中的方法getCost应用于给定类型:必需:double found:无参数原因:实际参数和形式参数列表在长度上有所不同。我已经将您的代码完全放在my IDE中,但它仍然在toString()中为litresUsed和cost提供错误;这是不可能的。你们重建应用程序了吗?我在你们发表评论之前重建过,现在似乎正在工作,非常感谢!