Java 如果语句使用字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19072806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:52:52  来源:igfitidea点击:

If statement using string

javastringif-statement

提问by

Okay, asked a question a while ago without providing all my code, dumb idea. Here it is. I'm trying to use an if statement with a string to get a cost. No matter what I do, my cost is getting returned as 0. See class CarRental, method getCost. I was told I could change it up and use a switch, which is fine, but I want to understand why what I'm doing isn't working anyway, for the sake of knowledge.

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

Update: Tried changing it to a switch, same results.

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

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

采纳答案by NoChance

You are not invoking the getCost method in your code.

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

回答by Stephen C

Your displaymethod is getting the "cost per day" from an instance variable called costin the LuxuryCarRental. None of your code assigns anything to that field, so (naturally!) it is always zero.

你的display方法是获得来自被称为实例变量“每日费用”costLuxuryCarRental。您的任何代码都没有为该字段分配任何内容,因此(自然!)它始终为零。