变量值未传递到另一个类中

huangapple go评论76阅读模式
英文:

Variable value not being passed into another class

问题

以下是翻译好的部分:

我正在尝试让用户选择他们菜单中的哪个选项,并根据客户选择的选项设置变量,但当我转到另一个类并检索它时,没有传递任何值。

这是我的 Alacarte 类:

public class Alacarte {
    public void alacarte() {
        Checkout c = new Checkout();
        System.out.println("请选择一份餐点");
        System.out.println("\n1. 炸鸡........9.90");
        System.out.println("2. 麦香鸡..........5.90");
        System.out.println("3. 辣味鸡腿汉堡......12.90");
        System.out.println("\n选项:");
        Scanner s = new Scanner(System.in);
        int option = s.nextInt();
        switch(option) {
            case 1:
                this.order = "炸鸡";
                this.price = 9.90;
                c.receipt();
                break;
            case 2:
                this.order = "麦香鸡";
                this.price = 5.90;
                break;
            case 3:
                this.order = "辣味鸡腿汉堡";
                this.price = 12.90;
                break;
        }
    }
    private String order;
    private double price;
    public double getPrice() {
        return this.price;
    }
    public String getOrder() {
        return this.order;
    }
}

这是我的 Checkout 类:

public class Checkout {
    public void receipt() {
        Alacarte as = new Alacarte();
        System.out.println("感谢您的订单");
        System.out.println("您的订单是: " + as.getOrder());
        System.out.println("价格为: " + as.getPrice());
        System.out.println("\n感谢您的订购!");
    }
}

这是我的 output

感谢您的订单
您的订单是: null
价格为: 0.0

感谢您的订购!
英文:

I'm trying to let the user to choose which option is in their menu, and setting the variables based on the option that the customer choose, but when I go to another class and retrieve it, there is no value being passed.

Here is my alacarte class

public class Alacarte {
    public void alacarte(){
        Checkout c = new Checkout();
        System.out.println("Please select a meal");
        System.out.println("\n1. Fried Chicken........9.90");
        System.out.println("2. McChicken..........5.90");
        System.out.println("3. Spicy Chicken McDeluxe......12.90");
        System.out.println("\nOption:");
        Scanner s = new Scanner(System.in);
        int option = s.nextInt();
        switch(option){
            case 1:
            this.order = "Fried Chicken";
            this.price = 9.90;
            c.receipt();
        
            case 2:
            this.order = "McChicken";
            this.price = 5.90;

            
            case 3:
            this.order = "Spicy Chicken McDeluxe";
            this.price = 12.90;
             
        }
    }
    private String order;
    private double price;
    public double getPrice(){
        return this.price;
    }
    public String getOrder(){
        return this.order;
    }

}

Here is my checkout class

public class Checkout {
     public void receipt(){
         Alacarte as = new Alacarte();
         System.out.println("Thank you for your order");
         System.out.println("Your order is: " + as.getOrder());
         System.out.println("The price is: " + as.getPrice());
         System.out.println("\nThank you for ordering with us!");
     }
}

Here is my output

Thank you for your order
Your order is: null
The price is: 0.0

Thank you for ordering with us!

答案1

得分: 1

你已经拥有所有信息

this.order = "Fried Chicken";
this.price = 9.90;
c.receipt();

所以更改receipt使其带有参数

this.order = "Fried Chicken";
this.price = 9.90;
c.receipt(this.order, this.price);

更改实现

public void receipt(String order, float price){
System.out.println("感谢您的订购");
System.out.println("您的订单是:" + order);
System.out.println("价格为:" + price);
System.out.println("\n感谢您的订购!");
}

英文:

You have all the information here

        this.order = "Fried Chicken";
        this.price = 9.90;
        c.receipt();

so change receipt so that it has parameters

        this.order = "Fried Chicken";
        this.price = 9.90;
        c.receipt(this.order, this.price);

Change the implementation

public void receipt(String order, float price){
     System.out.println("Thank you for your order");
     System.out.println("Your order is: " + order);
     System.out.println("The price is: " + price);
     System.out.println("\nThank you for ordering with us!");
}

答案2

得分: -1

你在收据方法中创建了一个新的Alacarte实例,该实例对用户输入一无所知。一种做法是将数据作为参数传递给收据方法。

c.receipt(this.order, this.price);

在结账部分:

public void receipt(String order, float price) {
    System.out.println("感谢您的订购");
    System.out.println("您的订单是:" + order);
    System.out.println("价格为:" + price);
    System.out.println("\n感谢您的订购!");
}

注意!在您的switch语句的末尾添加break语句,并在末尾添加default情况。

英文:

You are creating a new instance of Alacarte in you receipt method which does not know anything about the user input. One way to do this is to pass the data to the receipt method as parameters.

c.receipt(this.order, this.price);

And in Checkout:

public void receipt(String order, float price) {
    System.out.println("Thank you for your order");
    System.out.println("Your order is: " + order);
    System.out.println("The price is: " + price);
    System.out.println("\nThank you for ordering with us!");
}

NB! Add break statement at the end of your switch cases and default at the end.

huangapple
  • 本文由 发表于 2020年10月27日 16:06:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64550319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定