大家好。我是 Java 新手。我被困在我得到的输出上。我已经更改了连接,但问题仍然存在

问题描述 投票:0回答:1
public class HotelRoom {
    //declare variables
    private int age, numNightsStay;
    private  double price;
    private String msg, name;

    //constructor
    public HotelRoom(){
        age = 0;
        numNightsStay = 0;
        price = 90.0;
        msg ="";
        name="";
    }

    //set
    public void setAge(int age){
        this.age=age;
    }
    public void setNumNightsStay(int numNightsStay){
        this.numNightsStay=numNightsStay;
    }
    public void setName(String name){
        this.name=name;
    }

    //compute-process
    public void computePrice(){
        if(age <= 17){
            msg="Sorry you must be over 18 to stay here";
        }
        else {
            price=90.00*numNightsStay;
        }
    }

    //get
    public double getPrice(){
        return price;
    }
    public String getMsg(){
        return msg;
    }
}
import javax.swing.*;

public class HotelRoomApp {
    public static void main (String[] args){
        //declare variables
        int age, numNightsStay;
        double price;
        String name, msg;

        //declare and create objects
        HotelRoom hotel;
        hotel=new HotelRoom();

        //input
        age = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your age"));
        numNightsStay = Integer.parseInt(JOptionPane.showInputDialog(null, "How many nights do you want to stay"));
        name = JOptionPane.showInputDialog(null,"Enter your name");

        //set
        hotel.setAge(age);
        hotel.setNumNightsStay(numNightsStay);
        hotel.setName(name);

        //compute-process
        hotel.computePrice();

        //get
        price=hotel.getPrice();
        msg=hotel.getMsg();

        //output
        JOptionPane.showMessageDialog(null, " Hi " + name + " the cost is " + price + " euro" + msg);

    }
}

大家好。我是 Java 新手。我刚刚开始使用两个文件来编写我的代码。我遇到的问题是我的输出。该应用程序会记录用户的姓名、年龄以及他们将在酒店住宿的夜晚。他们必须年满 18 岁才能入住酒店。支出将给出他们希望入住的天数的总费用。当我输入 18 时,输出符合预期。我知道你的名字和总费用。但是,如果我输入 17 或以下,我会得到 hi name,则费用为 90.0。欧元 抱歉,您必须年满 18 岁才能留在这里。我以为这是我的串联,所以我重新编写了它,但我仍然遇到同样的问题。我对这个问题迷失了。我已经查看了文件,需要一些帮助。

我还在我的可实例化类中收到此警告“私有字段“名称”已分配但从未访问过”。我的理解是,对于我的应用程序类中的每个输入,我需要在可实例化类中设置一个方法。如果我删除了变量名称,那么我开始收到一大堆其他警告,例如从未使用参数“名称”。我应该在这里做什么。谢谢你

java concatenation concreteclass
1个回答
0
投票

name
问题很简单:您使用局部变量
name
来构建输出;使用
hotel.name
来引用 HotelRoom 对象的名称字段。

18秒以下的价格显示90的问题是因为你在构造函数中将价格设置为90:删除构造函数的

price = 90.0;
行。


我会从你的 HotelRoom 类中删除年龄检查;把它放在你的 main 方法中。

© www.soinside.com 2019 - 2024. All rights reserved.