【问题标题】:Is there a way to do this so I don't have 64 if statements?有没有办法做到这一点,所以我没有 64 个 if 语句?
【发布时间】:2019-11-15 23:47:59
【问题描述】:

有人告诉我从一个 txt 文件创建一个二维数组,该文件有“carModel,carColor”,后跟一个新行。 二维数组是 8x8,每次出现某个 carmore 和 carcolor 时,它们各自的 [x][y] 坐标代表它们的计数都会增加 1。

到目前为止,我已经读取了该文件,从该文件创建了一个二维数组,并创建了一个包含二维数组的输出,并且每个插槽都填充了零,但我似乎想出的唯一方法是更新每个模型,颜色计数是如果我手动制作 64 个 if 语句来检查它们是否出现在列表中 n+ 次。 肯定有其他方法吗?

例如,当我的扫描仪读取列表时,我需要它检查列表是否重复汽车型号、汽车颜色,如果是,则更新该汽车制造商和颜色的计数。

这是我到目前为止的代码:

public static void main (String [] args) throws Exception
{
    String [][] cars = new String [8][8]; 
    ArrayList <String> colors = new ArrayList<>();
    colors.add("BLUE  ");
    colors.add("BLACK ");
    colors.add("BROWN ");
    colors.add("GREEN ");
    colors.add("RED   ");
    colors.add("SILVER");
    colors.add("WHITE ");
    Collections.sort(colors);
    ArrayList <String> models = new ArrayList<>();
    models.add("Escape  ");
    models.add("Explorer");
    models.add("F150    ");
    models.add("F250    ");
    models.add("Flex    ");
    models.add("Mustang ");
    models.add("Taurus  ");
    Collections.sort(models);
    cars [0][0] = "_____  ";
    for (int a = 1; a < cars.length; a++)
    {
        cars[0][a] = (models.get(a-1)) + " ";
    }
    for (int b = 1; b < cars.length; b++)
    {
        cars[b][0] = (colors.get(b-1)) + " ";
    }
    for (int fir = 1; fir < cars.length; fir++)
    {
        for (int sec = 1; sec < cars[1].length; sec++)
        {
            if (cars[fir][sec] == null)
            {
                cars[fir][sec] = "0        ";
            }
        }
    }       
    for (int first = 0; first < cars.length; first++)
    {
        for (int second = 0; second < cars[first].length; second++)
        {
            System.out.print(cars[first][second]);
        }
        System.out.println();
    }        
    File file = new File ("C:\\Users\\delta\\Documents\\NetBeansProjects\\SchoolWork\\cars.txt");
    Scanner sc = new Scanner(file);
    String ab = sc.nextLine();
        while (ab != null)
        {
            String [] nums = sc.nextLine().split(" ");
            for (int i = 0; i < nums.length;i++)
            {                  
               System.out.println(nums[i]);                    
            }               
        }        
}

输出应该是这样的:

【问题讨论】:

  • Code Review问这个
  • 不确定是否有意义,但它可能会将您的模型和颜色覆盖到枚举并使用它们的序数作为数组索引。没有二次循环,只需扫描、拆分、Model.valueOf、Colour.valueOf、递增 martix[make.ordinal, Colour.ordinal]++。这假设您可以使用 int[] [] 而不是 string,并且枚举实际上对需求有意义。

标签: java arrays loops io


【解决方案1】:

您需要做的是使用ArrayList.indexOf() 方法。

  1. 获取颜色的索引。
  2. 获取模型的索引。

然后使用这些设置或增加 8x8 数组中的条目。每次阅读汽车的颜色和型号。


    int[][] count = new int[][];

    while (reading in data) {
       get color
       get model
       int row = modelList.indexOf(model)
       int col = colorList.indexOf(color)
       count[row][col]++
    }

我还建议您修剪颜色和模型周围的空白区域。它会使调试变得困难,而且你不需要它来格式化输出,只要你使用System.out.printf()

【讨论】:

  • 我不太确定你想说的话背后的逻辑或编码。我相信你的意思是我会做类似“对于 String[] nums 中的每个字符串 if each.equals(nums[i]) then cars [colors.indexOf(I)][models.indexOf[i] += 1”或也许是这样的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
  • 2020-11-17
  • 2021-07-09
  • 2020-06-26
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
相关资源
最近更新 更多