我目前正在为学校做一个小项目,我不知道为什么这会导致问题。
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;
}
}
trip calculator.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());
}
}
祝好运!