实例化构造函数后如何传递参数?

时间:2019-04-20 05:53:06

标签: java

我试图在实例化构造函数时将用户输入传递给参数。我被困在实例化构造函数时如何传递用户输入。我们在从另一个类继承的同时,使用用户输入来创建自定义炸玉米饼。

    import java.io.*;

    class TacoSupreme extends Taco
    {

    private int sourCream;
    private int blackOlives;
    private int userInput;

    public TacoSupreme()
    {
        super();
        System.out.println("Making a default Taco Supreme...");
        sourCream = 1;
        blackOlives = 1;


    }
    public int weight()
    {
        return (sourCream + blackOlives + super.weight());
    }
    public void gutBomb()

   {
        super.gutBomb();
        sourCream =  sourCream * 2;
        blackOlives =  blackOlives * 2;
    }

    public void print_order()
    {
        super.print_order();
        System.out.println("PLUS !!!!");
        System.out.println(sourCream + " units of sour cream, and");
        System.out.println(blackOlives + " units of black olives.");

    }
    public TacoSupreme (String n1,int b1,int b2,int c1,int l2, int s, int      o) throws IOException

    {
        super(n1, b1, b2, c1, l2);
        String user;
        BufferedReader br = new BufferedReader(new    InputStreamReader(System.in));       <---this works but I don't know how  to use the userInput when I instantiate -->
        System.out.print ("Please enter your name:  ");
        user = br.readLine();

        System.out.println("Making a custom Taco Supreme ...");
        sourCream = s;
        blackOlives = o;
        n1 = user;

    }

}
    public class Lab8
    {

    public static void main (String args[]) throws IOException
    {

        int w;
        TacoSupreme t0 = new TacoSupreme();
        t0.print_order();
        w = t0.weight();
        System.out.println("The above taco weight is: " + w + "\n");

        System.out.println("Invoking the gutBomb special!!!\n");
        t0.gutBomb();
        t0.print_order();
        w = t0.weight();
        System.out.println("The above taco weight is: " + w + "\n");


        TacoSupreme t1 = new TacoSupreme("bob",2,3,4,5,6,7);          <-----this is where I am having the problem---->I don't know how to add  the user input here

      t1.print_order();
        w = t1.weight();
        System.out.println("The above taco weight is: " + w + "\n");


        System.out.println("Invoking the gutBomb special!!!\n");
        t1.gutBomb();
        t1.print_order();
        w = t1.weight();
        System.out.println("The above taco weight is: " + w + "\n");

1 个答案:

答案 0 :(得分:1)

您可以使用Scanner类从用户那里获取输入并使用它们初始化constructor,并添加以下更改:

将此添加到Main():

Scanner scan = new Scanner(System.in);
System.out.print("Please enter name : ");
String name = scan.nextLine();
System.out.print("Please enter n1 : ");
int n1 = scan.nextInt();
System.out.print("Please enter b1 : ");
int b1 = scan.nextInt();
System.out.print("Please enter b2 : ");
int b2 = scan.nextInt();
System.out.print("Please enter c1 : ");
int c1 = scan.nextInt();
System.out.print("Please enter l1 : ");
int l1 = scan.nextInt();
System.out.print("Please enter o : ");
int o = scan.nextInt();

TacoSupreme t1 = new TacoSupreme(name,n1,b1,b2,c1,l1,o);

将此添加到TacoSupreme():

super(n1, b1, b2, c1, l2);
System.out.println("Making a custom Taco Supreme ...");
sourCream = s;
blackOlives = o;