【问题标题】:If statement using string [duplicate]If语句使用字符串[重复]
【发布时间】:2013-09-28 23:32:05
【问题描述】:

好的,前段时间问了一个问题,没有提供我所有的代码,愚蠢的想法。这里是。 我正在尝试使用带有字符串的 if 语句来获取成本。无论我做什么,我的成本都会返回为 0。请参阅 CarRental 类,getCost 方法。 有人告诉我我可以改变它并使用开关,这很好,但我想了解为什么我正在做的事情无论如何都不起作用,为了知识。

更新:尝试将其更改为开关,结果相同。

import java.util.*;

public class 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
}  //end class UseCarRental

class 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.");
        }

}

class LuxuryCarRental extends 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.");
        }
}

【问题讨论】:

  • 我还可以确认将其更改为开关并不能解决问题,显示时我仍然得到 0 作为成本。肯定有其他问题,但我看不出是什么问题。
  • "请输入您想要的汽车尺寸" 您会输入什么?在问另一个具有相同根原因的问题之前,请阅读所有 cmets 和答案。
  • 提示:插入对System.out.println 的调用以在代码的不同位置输出值。例如,在第一个 if 语句之前插入一个以输出“size”:System.out.println("Size = " + size);

标签: java string if-statement


【解决方案1】:

您没有在代码中调用 getCost 方法。

【讨论】:

    【解决方案2】:

    您的display 方法正在从LuxuryCarRental 中名为cost 的实例变量中获取“每日成本”。您的任何代码都没有为该字段分配任何内容,因此(自然!)它始终为零。

    【讨论】:

    • 如果我运行程序,即使我选择其他三种汽车尺寸之一,我也会得到 0。
    • @E.Watson - 我的回答解释了这一点。你得到零因为字段总是包含零。它总是包含零因为你没有分配任何东西给它。你的getCost() 方法是无关紧要的如果你不使用它
    猜你喜欢
    • 1970-01-01
    • 2016-05-15
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    相关资源
    最近更新 更多