关于“if”声明的快速问题

时间:2013-09-28 23:07:04

标签: java if-statement

我在这里制作的这个小方法并没有返回正确的成本值。我想知道我是否错误地使用了if语句。由于我是Java的新手,这是我第一次使用带有字符串的if语句。我没有书或老师,只是自己学习。任何帮助将不胜感激。

编辑:发布完整代码

import java.util。*;

公共类UseCarRental {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Thank you for choosing ICT car Rentals\n"
            + "Pleae enter your full name:");
    String renterName = input.nextLine();
    System.out.println("Please enter your zip code:");
    int renterZipcode = input.nextInt();
    input.nextLine();
    System.out.println("Please enter the size car you would like:\n"
            + "economy\n"
            + "midsize\n"
            + "fullsize\n"
            + "luxury?");
    String carSize = input.next();
    System.out.println("How many days do you wish to rent this?");
    int rentalDays = input.nextInt();

    if (carSize.equals("luxury")) {
        System.out.println("Will you be wanting a chauffer (y or n");
        String chauffer = input.next();
        LuxuryCarRental rentIt = new LuxuryCarRental(renterName, 
        renterZipcode, carSize, rentalDays,chauffer);
        rentIt.display();
    } else {
        CarRental rentIt = new CarRental(renterName, renterZipcode, 
        carSize, rentalDays);
        rentIt.display();
    }




} //  end main method

} //结束类UseCarRental

类CarRental {

private int days;
private int zip;
private double cost;
private String size;
private double total;
private String name;

    CarRental(String renterName, int renterZipcode, String carSize, int rentalDays){
        this.days = rentalDays;
        this.zip = renterZipcode;
        this.name = renterName;
        this.size = carSize;
    }

    double getCost(){
        if(size.equals("economy")){
            cost = 29.99;
        }
        if(size.equals("midsize")){
            cost = 38.99;
        }
        if(size.equals("fullsize")){
            cost = 43.50;
        }
        return cost;
    } 

    void display(){
        System.out.println("Thank you for using our service.");
        System.out.println("Your order is as follows:");
        System.out.println("Name: " + name);
        System.out.println("Zip code: " + zip);
        System.out.println("Car size: " + size);
        System.out.println("Cost per day: " + cost);
        System.out.println("Days requested: " + days);
        total = days * cost;
        System.out.println("Total cost: " + total);
        System.out.println("If any of the above information is incorrect, too bad bud, because it isn't.");
    }

}

类LuxuryCarRental扩展了CarRental {

private int chauffer = 200;
private int days;
private int zip;
private double cost;
private String size;
private double total;
private String name;

LuxuryCarRental(String renterName, int renterZipcode, String carSize, int rentalDays, String chauffer){
    super(renterName, renterZipcode, carSize, rentalDays);
    this.days = rentalDays;
    this.zip = renterZipcode;
    this.name = renterName;
    this.size = carSize;
}

@Override
void display(){
        System.out.println("Thank you for using our service.");
        System.out.println("Your order is as follows:");
        System.out.println("Name: " + name);
        System.out.println("Zip code: " + zip);
        System.out.println("Car size: Luxury");
        System.out.println("Cost per day: " + cost);
        System.out.println("Days requested: " + days);
        System.out.println("Chauffer cost: " + chauffer);
        total = days * cost + chauffer;
        System.out.println("Total cost: " + total);
        System.out.println("If any of the above information is incorrect, too bad bud, because it isn't.");
    }

}

5 个答案:

答案 0 :(得分:0)

这里可能发生的是size不是“中型”,“全尺寸”或“经济”。解决此问题的一种快速方法是将此行添加到函数的开头:cost = 9001;//or whatever number you want

答案 1 :(得分:0)

假设sizeString,您就是正确的方式。您还可以使用String.equalsIgnoreCase(String)来比较字符串,而不考虑大写/小写。

请记住,size在函数内部未定义,您需要在函数属性中传递它才能被视为

double getCost(String size)

除此之外,你正走在正确的道路上。

答案 2 :(得分:0)

当您拥有互斥式if语句时,通常希望使用if - else - if链:

double getCost(){
    if(size.equals("economy")){
        cost = 29.99;
    }
    else if(size.equals("midsize")){
        cost = 38.99;
    }
    else if(size.equals("fullsize")){
        cost = 43.50;
    }
    return cost;
}

在你的情况下,这不是一个问题,但它通常是一个很好的做法,因为它更有效(只有很少的工作),更重要的是 - 你可以保证只执行一个分支,这在内部代码很重要分支可以更改后面条件表达式的值。

无论如何,你的代码有一个更严重的问题:getCost不仅返回成本 - 它还设置它!这是一个大问题,因为为了使display正常工作,需要在getCost之前调用display。这是一种非常奇怪的行为,即使你记录它也会令人困惑和错误修剪。

你应该做的是完全摆脱cost变量。 display,而不是直接使用cost,应使用getCost来计算费用:

System.out.println("Cost per day: " + getCost());

对于getCost,不应设置cost(不再存在),而应直接返回费用:

double getCost(){
    if(size.equals("economy")){
        return 29.99;
    }
    else if(size.equals("midsize")){
        return 38.99;
    }
    else if(size.equals("fullsize")){
        return 43.50;
    }
    throw new RuntimeException(String.format("'%s' is not a valid size.", size));
}

请注意,在这种情况下,您不需要else - 但保留它们并没有什么坏处,这对于保持一致性很有帮助。

答案 3 :(得分:0)

这取决于大小和成本是否是包含此方法的类的实例变量,我假设它们是或者此代码甚至不能编译,在这种情况下问题可能是因为字符串大小不相等“经济”,“中型”或“全尺寸”。

要考虑的一件事是将大小设为枚举,从而将其限制为仅有效值

public enum Size {
ECONOMY(29.99), MIDSIZE(38.99), FULLSIZE(43.50);

private double cost;

Size(double cost) {
    this.cost = cost;
}

public double getCost() {
    return cost;
}       

}

或者,如果大小和成本不是您的类的实例变量,您可以编写像这样的函数方法

public static double getCost(String size){
    double cost; 
    if(size.equals("economy")){
        cost = 29.99;
    } else if(size.equals("midsize")){
        cost = 38.99;
    } else if(size.equals("fullsize")){
        cost = 43.50;
    }
    return cost;
}

如果我们已经确定它是经济规模,请注意使用 else if 来阻止检查fullsize / midsize

答案 4 :(得分:0)

如果您的String carSize = input.next();有一个“奢侈”价值,那么你将无法获得getCost,你将创造一辆豪华车

LuxuryCarRental(String renterName, int renterZipcode, String "luxury", int rentalDays, String chauffer){
    super(renterName, renterZipcode, "luxury", rentalDays);

...

double getCost(){
    if(size.equals("economy")){
        cost = 29.99;
    }
    else if(size.equals("midsize")){
        cost = 38.99;
    }
    else if(size.equals("fullsize")){
        cost = 43.50;
    }
    return cost; //if luxury???
}

可能会有所帮助:

LuxuryCarRental(String renterName, int renterZipcode, String carSize, int rentalDays, String chauffer){
    super(renterName, renterZipcode, "fullsize", rentalDays);

或者创造豪华车的主要方法是传递另一个参数,或者为奢侈品添加另一个值。或者更好,使用ENUM,因此您无法传递无效值。

一般情况下,无法保证传递有效值。您必须使用ENUM处理该验证,或强制用户输入4个有效值中的一个并再次询问他是否提供其他内容...如果案例不重要则添加忽略案例“经济”不等于“经济”

最后,

System.out.println("Cost per day: " + cost);

必须是这样的:

System.out.println("Cost per day: " + getCost());

由于您从未在构造函数或其他任何位置设置成本。