【问题标题】:How to create a class and add to a list in a for loop receiving the input from console如何在从控制台接收输入的for循环中创建一个类并添加到列表中
【发布时间】:2026-02-13 04:40:02
【问题描述】:

我有一个类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));
        }

    }

}

【问题讨论】:

    标签: java class object constructor


    【解决方案1】:

    使用Scanner 从控制台读取输入(由空格和不同行上的不同项目分隔)。将此插入您的代码中(而不是硬编码值)

        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());
    

    【讨论】:

    • ProductMaster milk= new ProductMaster("Milk",3.97,2,5.00); ProductMaster 面包= new ProductMaster("面包",2.17,3,6.00); ProductMaster 香蕉= new ProductMaster("香蕉",0.99,0,0.0);
    • @RonHilkin 这里有什么问题?
    • 1..ProductMaster milk= new ProductMaster("Milk",3.97,2,5.00); 2..ProductMaster 面包= new ProductMaster("面包",2.17,3,6.00); 3..ProductMaster 香蕉= new ProductMaster("香蕉",0.99,0,0.0); 4..list.add(牛奶); 5..list.add(面包); 6..list.add(香蕉); @HumbleFoolish 在相同的场景中,整个 1-6 行,它们可以使用 for loop for { //using bufferedreader String n1=.. Double n2=.. ProductMaster n1= new ProductMaster(n1,n2,n3, n4); list.add(new ProductMaster(n1,n2,n3,n4));
    • @RonHilkin 我已经编辑了代码以使其更加清晰,并在 for 循环之外添加了列表的初始化。因此,您评论中的第 1-6 行由 for 循环自动执行。而且,在 for 循环结束后,您的列表应该有 3 个产品(如果您从控制台输入 3 行)
    【解决方案2】:
      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);
                }
    

    【讨论】:

      最近更新 更多