Java 实例化构造函数后如何传入参数?
我试图在实例化构造函数时将用户输入传递到参数中。当我们实例化构造函数时,我被困在如何传递用户输入上。我们在从另一个类继承时使用用户输入来创建自定义tacoJava 实例化构造函数后如何传入参数?,java,Java,我试图在实例化构造函数时将用户输入传递到参数中。当我们实例化构造函数时,我被困在如何传递用户输入上。我们在从另一个类继承时使用用户输入来创建自定义taco import java.io.*; class TacoSupreme extends Taco { private int sourCream; private int blackOlives; private int userInput; public TacoSupreme(
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");
import java.io.*;
TacoSupreme类扩展了Taco
{
私有内部sourCream;
私人黑橄榄;
私有int用户输入;
公共塔科苏普雷姆酒店
{
超级();
System.out.println(“制作默认的Taco Supreme…”);
sourCream=1;
黑橄榄=1;
}
公共整数权重()
{
返回(sourCream+黑橄榄+超级重量());
}
公共炸弹
{
super.gutBomb();
sourCream=sourCream*2;
黑橄榄=黑橄榄*2;
}
公开作废印刷单()
{
super.print_order();
System.out.println(“PLUS!!”);
System.out.println(sourCream+“酸奶油单位,和”);
System.out.println(黑橄榄+“黑橄榄单位”);
}
公共TacoSupreme(字符串n1、intb1、intb2、intc1、intl2、ints、into)抛出IOException
{
超级(n1,b1,b2,c1,l2);
字符串用户;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));您可以使用扫描仪类从用户获取输入并初始化构造函数,使用它们添加以下更改:
将此添加到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);
super(n1, b1, b2, c1, l2);
System.out.println("Making a custom Taco Supreme ...");
sourCream = s;
blackOlives = o;
将此添加到TacoSupreme():
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);
super(n1, b1, b2, c1, l2);
System.out.println("Making a custom Taco Supreme ...");
sourCream = s;
blackOlives = o;
用户输入来自何处?它们是在运行程序时作为参数传递的,还是要通过stdin获取?