【问题标题】:Can I use a mixture of strings and ints in a 2D array in Java?我可以在 Java 的二维数组中混合使用字符串和整数吗?
【发布时间】:2023-01-18 21:12:22
【问题描述】:

我正在写一个披萨订购程序。 这些是我正在使用的变量:

Topping             Measure        Max Quantity  
         
Pizza cheese        1/4 cup    two
Diced onion     1/8 cup    two
Diced green pepper  1/8 cup    two
Pepperoni       2 pieces       Four
Sliced mushroom     1/8 cup    Two
Diced jalapenos     1/8 cup    Two
Pineapple Chunks    2 pieces       Four
Tofu                1/4 cup    Two 
Ham Chunks      4 pieces       Four
Dry red pepper  Generous sprinkle  Four
Dried basil Generous sprinkle  Two

“测量”真的可以命名为任何东西。我希望程序向用户列出浇头,让他们选择一个,询问数量,然后询问用户是否要添加更多浇头(while 循环)。我刚刚学会了如何使用数组,但这个项目对我来说更高级。我应该为此任务使用多个数组还是二维数组?

我尝试使用 arrayList 时吃得太多了,结果把自己弄糊涂了。

【问题讨论】:

  • 看起来你真正需要的是一个对象。

标签: java arrays


【解决方案1】:

我同意您为此需要一个对象。我试着想一些使用二维数组的东西。

    String[][] toppings = new String[3][3];
    // First column -> Topping
    // Second column -> Measure
    // Third column -> Max Quantity
    
    // Input
    // Topping-1 2 8
    // Topping-2 3 9
    // Topping-3 2 10
    fillMatrix(scanner, toppings, 3, 3);

    System.out.println("Please select toppings");
    int n = 1;
    for (String[] topping : toppings) {
        System.out.println("For " + topping[0] + " press " + n);
        n++;
    }
}

private static void fillMatrix (Scanner scanner, String[][]matrix, int rows, int cols) {
    for(int row = 0; row < rows; row++) {
        matrix[row] = scanner.nextLine().split("\s+");
    }
}

}

您可以将选定的浇头存储在数组中。

【讨论】:

    【解决方案2】:

    Arrays 确实适用于相同类型元素的固定大小集合。所以,不,你不能在这里使用数组来存储“浇头”的不同“属性”

    您可以使用类来对不同类型的数据元素进行分组。

    public enum ToppingUnit {
      CUP, PIECE, GENEROUS_SPRINKLE;
    
      public String getName() {
        return name().replace("_", " ");
      }
    }
    
    public class Topping {
      private String name;
      private int measure;
      private int maxQuantity;
    
      private ToppingUnit unit;
    
      public Topping(String name, int measure, ToppingUnit unit, int maxQuantity) {
        this.name = name;
        this.measure = measure;
        this.maxQuantity = maxQuantity;
        this.unit = unit;
      }
    
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
      public int getMeasure() {
        return measure;
      }
    
      public void setMeasure(int measure) {
        this.measure = measure;
      }
    
      public int getMaxQuantity() {
        return maxQuantity;
      }
    
      public void setMaxQuantity(int maxQuantity) {
        this.maxQuantity = maxQuantity;
      }
    
      public ToppingUnit getUnit() {
        return unit;
      }
    
      public void setUnit(ToppingUnit unit) {
        this.unit = unit;
      }
    }
    
    
    public class Main {
    
      public static void main(String[] args) {
        Topping toppings[] = {
            new Topping("Pizza cheese", 1 / 4, ToppingUnit.CUP, 2),
            new Topping("Diced onion", 1 / 8, ToppingUnit.CUP, 2),
            new Topping("Diced green pepper", 1 / 8, ToppingUnit.CUP, 2),
            new Topping("Pepperoni", 2, ToppingUnit.PIECE, 4),
            new Topping("Sliced mushroom", 1 / 8, ToppingUnit.CUP, 2),
            new Topping("Diced jalapenos", 1 / 8, ToppingUnit.CUP, 2),
            new Topping("Pineapple Chunks", 2, ToppingUnit.PIECE, 4),
            new Topping("Tofu", 1 / 4, ToppingUnit.CUP, 2),
            new Topping("Ham Chunks", 4, ToppingUnit.PIECE, 4),
            new Topping("Dry red pepper", 1, ToppingUnit.GENEROUS_SPRINKLE, 4),
            new Topping("Dried basil", 1, ToppingUnit.GENEROUS_SPRINKLE, 2)};
      }
    }
    

    所以现在你有了 Toppings 的数组。你可以用它做任何你想做的事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      相关资源
      最近更新 更多