关于“if”语句的快速问题

2024-02-13

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

编辑:发布完整代码

导入 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

类汽车租赁{

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

}


假设size is a String,你走对了路。您还可以使用String.equalsIgnoreCase(String)比较字符串而不考虑大写/小写。

请记住size在函数内部未定义,您需要将其传递到要考虑的函数属性中

double getCost(String size)

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关于“if”语句的快速问题 的相关文章

  • JavaScript 无法解析 Java 中使用 Gson 序列化的字符串[重复]

    这个问题在这里已经有答案了 这不是重复的这个问题 https stackoverflow com q 15637429 274677因为这里的问题具体是关于在 JavaScript 中以文字形式从 Java 序列化的 JSON 字符串的表示
  • 项目展开时自动调整列大小

    在具有多列的树中 如何在展开 折叠和数据更新时调整列的大小以适应内容 类似的解决方案表格问题 https stackoverflow com questions 3186340 swt table auto resize all colum
  • struts2应用程序未运行

    我是 Maven 和 struts2 的新手 当我尝试运行我的程序时出现以下错误 严重 调度程序初始化失败 无法加载配置 操作 文件 E workspace metadata plugins org eclipse wst server c
  • 如何使用 selenium 和 junit 测试多个浏览器(版本)

    我刚刚发现了硒 一个很棒的工具 我计划运行 使用 selenium ide 生成的 junit4 代码 但我需要它与许多浏览器 网络驱动程序一起运行 这个用例有 junit java 模式吗 我的第一个想法是使用 RunWith Param
  • 如何使用我的 Apple 开发者 ID 签署 .jar 文件

    我有一个java可执行文件jar我需要使用我的 Apple 开发者 ID 进行签名的文件 我不打算通过应用程序商店分发它 我将通过我的网站直接向客户分发该应用程序 我不打算绕过 Gatekeeper Signing the app和我的de
  • Spring:如何将 KeyHolder 与 PostgreSQL 一起使用

    最近迁移到 POSTGRESQL 我试图获取在数据库表中创建新条目时唯一生成的密钥 桌子screenstable看起来像这样 CREATE TABLE screenstable id serial NOT NULL screenshot b
  • LoggerFactory.getLogger(ClassName.class) 与 LoggerFactory.getLogger(this.getClass().getName())

    我正在努力提高我的 Java 优化技能 为了实现这一目标 我制作了一个旧程序 并且正在尽力使其变得更好 在此程序中 我使用 SL4J 进行日志记录 为了获取记录器 我做了 private static final Logger logger
  • 如何将 csv/文本文件从 Android 手机发送到 WiFi 打印机?

    我正在开发一个 Android 应用程序 我应该从数据库中获取 csv txt 文件格式的数据 然后我必须将文件发送到 wifi 打印机 有谁知道我如何开始这样做 答案终于很简单 Socket client new Socket IP PO
  • 从文件夹中读取java文件

    我开发了一个应用程序 可以从用户选择的文件夹中读取文件 它显示每个文件中有多少行代码 我只想在文件选择器中显示 Java 文件 具有 java 扩展名的文件 下面是我的代码 public static void main String ar
  • 我的递归条件是否正确计算二叉树高度?

    我想在你的帮助下知道我的代码是对还是错 因为遗憾的是我无法运行它来检查 没有编译错误 我想做的是找到二叉树的高度 当然 树不必是平衡的 二叉树中的每个节点可以有两个节点作为子节点 http en wikipedia org wiki Bin
  • 接受 05/05/1999 和 5/5/1999 等的日期时间解析

    有没有一种简单的方法来解析可能为 MM DD yyyy M D yyyy 或某种组合的日期 即 在一位数字的日期或月份之前 零是可选的 要手动执行此操作 可以使用 String dateFields dateString split int
  • 使用 jsch 将远程服务器中的目录移动到同一远程服务器中的另一个位置

    我正在使用 JSCH API 通过 sftp 连接到远程服务器 我需要将远程服务器中的目录从位置 A 移动到位置 B 看起来像方法channelsftp put src dest 只允许移动文件而不移动目录 有人可以解释一下我该怎么做吗 您
  • Spring MVC @RequestBody 不适用于 jquery ajax?

    这是我的ajax请求 var dataModel name1 value1 name2 value2 ajax url testURL type POST async false contentType application json d
  • Apache PDFBox 旋转 PDImageXObject

    我正在使用 2 0 0 SNAPSHOT 我想将页面设置为横向并旋转我的图片 所以我已经做到了page setRotation 90 使用 PDFBox 和 AffineTransform 似乎存在错误 这段代码没有做任何我期望的事情 Af
  • 纹理映射和光照顶点着色器错误 Java OpenGL

    我正在尝试将纹理映射到 3D 立方体并尝试编写着色器以使其具有照明和纹理 我尝试过只编写纹理着色器并且它有效 我还尝试过仅将 3D 值设置为红色的照明着色器 并且该照明着色器也可以工作 但当我尝试将两者结合起来时 我遇到了问题 我在下面提供
  • 从 java 反射中隐藏我的安全密钥

    下面的类是我用于加密的安全密钥提供程序 public class MySecretKey private String key 2sfdsdf7787fgrtdfg cj5 Some Util methods goes on Here 首先
  • 将任何文件读取为二进制字符串

    正如标题所示 有什么方法可以读取 Java 或任何其他语言 中给定文件 txt docx exe 等 的二进制表示形式 在java中 我知道如何按原样读取文件的内容 即 String line BufferedReader br new B
  • JProgressBar 不在循环内更新

    我目前正在自学Java 我一直在尝试不同的事情 比如JRadioButtons JcomboBoxes 等 现在 我正在尝试使用JProgressBar但它似乎无法正常工作 相关代码段 JProgressBar progress JButt
  • 客户端和服务器之间的安全连接

    我正在开发一个服务器组件 它将为嵌入式客户端的请求提供服务 这也在我的控制之下 现在一切都是测试版 安全性是这样的 客户端通过 https 发送用户名 密码 服务器返回访问令牌 客户端使用自定义标头中的访问令牌通过 http 发出进一步请求
  • Hibernate 对于 Android 应用程序来说是一种杀伤力吗? [复制]

    这个问题在这里已经有答案了 我正在为我的 Android 应用程序寻找一个好的 ORM 乍一看似乎对于移动设备我更喜欢使用更简单的东西 问题是我只是在这里假设 没有真正的证据 所以我想我应该询问社区的意见 也许有人有过这样的经历 它是一个相

随机推荐