有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java中向arraylist添加多个值

我还是Java新手,我已经创建了许多ArrayList,我想从不同的方法调用它们。我已经多次使用while循环来运行代码。我想知道是否有人能帮我解决我的问题。 在代码停止运行之前,用户应该能够添加任意数量的比萨饼

public static void main(String[] args) {
           boolean test = true;
         while(test == true){    
          System.out.println("Please press 1 to start a new order, 2 to update an order or 3 to delete 0 to finish");
        Scanner inputValue = new Scanner(System.in);
          String value = inputValue.nextLine();
          int result = Integer.parseInt(value);
         Orders order = new Orders();
             int number = result;
             if (number == 1){
                order.add();
             }
             if (number == 2){
                 order.update();
             }
             if(number == 3){
                 order.delete();
             }
             if(number == 0){
                 order.calculatePizzas();
                test = false;
             }
         }


    }

秩序。添加()

public ArrayList <String>  pizzaSizeA = new ArrayList<>();
public ArrayList<Double> sizePriceA = new ArrayList<>();
public ArrayList<String> crustName = new ArrayList<>();
public ArrayList<Double> crustPrice = new ArrayList<>();
  public ArrayList<Double> base = new ArrayList<>();
  public ArrayList<String> sauceName = new ArrayList<>();
  public ArrayList<Double> saucePrice = new ArrayList<>();
  public ArrayList<String> topping1 = new ArrayList<>();
 public  ArrayList<Double> t1Price = new ArrayList<>();
 public ArrayList <String> topping2 = new ArrayList<>();
 public  ArrayList<Double> t2Price = new ArrayList<>();
 public  ArrayList<Integer> Quantity = new ArrayList<>();
   // Add the pizzas so that they get added to the Array.
          System.out.println("Please enter a Base?");
          Scanner baseSize = new Scanner(System.in);
          String baseSize1 = baseSize.nextLine();
          JavaApplication4.Pizza Basesize1 = new JavaApplication4.Pizza();
          Basesize1.Size(baseSize1);
          pizzaSizeA.add(baseSize1);

          //Enter the crust you would like
          System.out.println("\n Please enter your crust?");
          Scanner crustty = new Scanner(System.in);
          String crustt = crustty.nextLine();
          JavaApplication4.Pizza crustPizza = new JavaApplication4.Pizza();
          JavaApplication4.Pizza.crust(crustt);
          crustName.add(crustt);

          // calculate the base price
          JavaApplication4.Pizza basePrice = new JavaApplication4.Pizza();
          JavaApplication4.Pizza.base();
          //Sauce price
          System.out.println("Please enter a Sauce?");
          Scanner cSauce = new Scanner(System.in);
          String cSauce1 = cSauce.nextLine();
          JavaApplication4.Pizza csauce = new JavaApplication4.Pizza();
           JavaApplication4.Pizza.sauce(cSauce1);
           sauceName.add(cSauce1);
           //Choose topping
           System.out.println("Choose a topping you would like to add?");
           Scanner top = new Scanner(System.in);
           String toppin = top.nextLine();
           JavaApplication4.Pizza to0pping = new JavaApplication4.Pizza();
           JavaApplication4.Pizza.topping1(toppin);
           topping1.add(toppin);
           //Choose topping 2

           System.out.println("Choose your second topping?");
           Scanner top2 = new Scanner(System.in);
           String toppin2 = top2.nextLine(); 
           JavaApplication4.Pizza to0pping2 = new JavaApplication4.Pizza();
           JavaApplication4.Pizza.topping2(toppin2);
           topping2.add(toppin2);

           //Quantity
           System.out.println("Please enter quantity");
           Scanner qunatity = new Scanner(System.in);
           String quan = qunatity.nextLine();
           int q = Integer.parseInt(quan);
           Orders.this.Quantity.add(q);
           System.out.println(Quantity);
    ````````````````````````````````````````````````

共 (1) 个答案

  1. # 1 楼答案

    我认为它适合你:

    List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
    

    你可以这样做:

    List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
    myArray.add(stooges);
    

    还有这样一个把戏:

    myArray.add(Arrays.asList("Larry", "Moe", "Curly"););
    

    下面是一些非常有用的代码示例的链接。好的例子有助于学习

    https://github.com/in28minutes/java-a-course-for-beginners

    祝你Java学习好运