Java 如何在接收控制台输入的for循环中创建类并添加到列表中
我想创建一个类Java 如何在接收控制台输入的for循环中创建类并添加到列表中,java,class,object,constructor,Java,Class,Object,Constructor,我想创建一个类ProductMaster,并将其添加到接收控制台输入的for循环中的列表中 课程如下: public class ProductMaster { String productName; double productPrice; // ... so on ProductMaster() { } ProductMaster(String prodN, double prodP, int discQ, double discP) {
ProductMaster
,并将其添加到接收控制台输入的for循环中的列表中
课程如下:
public class ProductMaster {
String productName;
double productPrice; // ... so on
ProductMaster() { }
ProductMaster(String prodN, double prodP, int discQ, double discP) {
productName=prodN;
// ...
}
public static void main(String[] args) {
// ... hardcoded values for example
// ... trying to achieve this dynamically by using a for loop,
// ProductMaster milk = new ProductMaster("Milk",3.97,2,5.00)
// list.add(milk);
// trying to create the above two lines by using for loops
for (int i=0; i<n; i++) {
System.out.println("Enter the item name:");
String n1 = br.readLine();
System.out.println("Enter the item price:");
Double n2 = Double.parseDouble(br.readLine());
// taking input from console
ProductMaster n12 = new ProductMaster(n12, n2, n3, n4);
list.add(new ProductMaster(n12, n2, n3, n4));
}
}
}
公共类ProductMaster{
字符串名称;
双倍产品价格;/…依此类推
ProductMaster(){}
ProductMaster(字符串prodN、双prodP、int discQ、双discP){
productName=prodN;
// ...
}
公共静态void main(字符串[]args){
//…例如,硬编码值
//…试图通过使用for循环来动态实现这一点,
//ProductMaster milk=新ProductMaster(“milk”,3.97,2,5.00)
//添加(牛奶);
//尝试使用for循环创建上述两行
对于(int i=0;i使用扫描仪
从控制台读取输入(由空格和不同行上的不同项目分隔)。将其插入代码(而不是硬编码值)
Scanner sc=新扫描仪(System.in);
字符串[]strArray;
列表=新的ArrayList();
while(sc.hasNextLine()){
strArray=sc.nextLine().split(\\s+);//考虑空格分隔的值
ProductMaster ProductMaster=新ProductMaster(
strArray[0]、Double.parseDouble(strArray[1])、Integer.parseInt(strArray[2])、Double.parseDouble(strArray[2]);
列表。添加(productMaster);
}
sc.close();
//列表大小
System.out.println(list.size());
ArrayList list=new ArrayList();
System.out.println(选择);
试一试{
if(choice.toLowerCase().equals(“yes”))
{
System.out.println(“您输入的是,现在请输入新项目的数量”);
n=Integer.parseInt(br.readLine());
System.out.println(“您已选择输入项目”+n);
对于(INTI=0;IPProductMaster milk=new ProductMaster(“milk”,3.97,2,5.00);ProductMaster面包=new ProductMaster(“bread”,2.17,3,6.00);ProductMaster香蕉=new ProductMaster(“banana”,0.99,0,0.0);@RonHilkin这里有什么问题?1.ProductMaster milk=new ProductMaster(“milk”,3.97,2,5.00);2..ProductMaster bread=new-ProductMaster(“bread”,2.17,3,6.00);3..ProductMaster banana=new-ProductMaster(“banana”,0.99,0,0.0);4..list.add(milk);5..list.add(bread);6..list.add(banana);@HumbleFoolish在同一场景中,整个1-6行可以使用for循环动态生成吗{//使用bufferedreader字符串n1=…Double n2=…ProductMaster n1=新ProductMaster(n1,n2,n3,n4);list.add(新ProductMaster(n1,n2,n3,n4))@RonHilkin我对代码进行了编辑,使其更加清晰,并添加了for循环外部列表的初始化。因此,for循环会自动执行注释中的第1-6行。并且,在for循环结束后,列表应该有3个产品(如果从控制台输入3行)
Scanner sc = new Scanner(System.in);
String[] strArray;
List<ProductMaster> list = new ArrayList<>();
while(sc.hasNextLine()){
strArray = sc.nextLine().split("\\s+"); //considering space separated values
ProductMaster productMaster= new ProductMaster(
strArray[0], Double.parseDouble(strArray[1]),Integer.parseInt(strArray[2]),Double.parseDouble(strArray[2]));
list.add(productMaster);
}
sc.close();
//list size
System.out.println(list.size());
ArrayList<ProductMaster> list=new ArrayList<ProductMaster>();
System.out.println(choice);
try{
if(choice.toLowerCase().equals("yes"))
{
System.out.println("You entered yes, Now please enter number of new items");
n=Integer.parseInt(br.readLine());
System.out.println("You have chosed to enter items"+n);
for(int i=0;i<n;i++)
{
System.out.println("Enter the item name:");
String n1=br.readLine();
System.out.println("Entered:"+n1);
System.out.println("Enter the item price:");
Double n2=Double.parseDouble(br.readLine());
System.out.println("Entered:"+n2);
System.out.println("Enter the item sale quantity");
n3 =Integer.parseInt(br.readLine());
System.out.println("Entered:"+n3);
System.out.println("Enter the item sale price");
Double n4=Double.parseDouble(br.readLine());
System.out.println("Entered:"+n4);
ProductMaster newC= new ProductMaster(n1,n2,n3,n4);
list.add(newC);
}