简体   繁体   English

如何显示二维数组中的最大数量?

[英]How can I display the largest amount in my 2D array?

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Random;

public class labb8 {
  public static void main(String[] args) {
    Random rnd = new Random();
    int[][] sales = new int[5][7];
    int[] total = new int[sales.length];
    ArrayList<String> unpopularSoftware = new ArrayList<String>();

    String[] locations = {"Houston", "Dallas", "Hunts", "San Ant", "Austin"};

    System.out.println("Location\t| Software Sales\t\t| Total Sales");
    System.out.println("--------------------------------------------------");

    for (int i = 0; i < sales.length; i++) {
      for (int j = 0; j < sales[i].length; j++) {
        sales[i][j] = rnd.nextInt(25);
        total[i] += sales[i][j];
      }
      System.out.print(locations[i] + "\t\t|");
      System.out.print(" " + Arrays.toString(sales[i]));
      System.out.println("\t| " + total[i]);
    }

    int unpopularModelCounter;
    for (int j = 0; j < sales[0].length; j++) {
      unpopularModelCounter = 0;
      for (int i = 0; i < sales.length; i++) {
        if (sales[i][j] != 0) {
          unpopularModelCounter++;
        }
      }
      if (unpopularModelCounter <= 3) {
        unpopularSoftware.add("Software " + (j + 1));
      }
    }
    System.out.println("Unpopular Software: " + unpopularSoftware);
    System.out.println("Location with most sold licenses: ");
  }
}

Above is the code and it gives me the results I'm looking for;以上是代码,它给了我正在寻找的结果; however, I'd like to know some methods on how can I print out the name of the location that has the most sold software?但是,我想知道一些关于如何打印出拥有最多销售软件的位置的名称的方法? What I've tried is putting System.out.println(total[i]);我试过的是把System.out.println(total[i]); under System.out.println("\t| " + total[i]);System.out.println("\t| " + total[i]); but that just displayed all the totals, which is what I figured would happen.但这只是显示了所有总数,这是我认为会发生的。 I've also tried putting System.out.println(total[i]);我也试过把System.out.println(total[i]); where the other output lines are at the bottom(which is where I want it), but the code can't find [i], which made me believe that I might have to create some methods;其他 output 行在底部(这是我想要的位置),但代码找不到 [i],这让我相信我可能必须创建一些方法; so, again, I'm asking for some course of advice on how to print out the name of the city with the largest amount sold in terms of my code.因此,我再次要求就如何打印出就我的代码而言销售量最大的城市名称提供一些建议。

If you want the name of the city the the highest total, you need to look through the total array for the largest total and, while doing that, keep track of both the largest total you've seen so far and the index at which you saw that largest total.如果您希望城市名称的总数最高,则需要查看total组中的最大总数,同时跟踪您迄今为止看到的最大总数和您所在的索引看到了最大的总数。

Something like this should do the job nicely:像这样的东西应该可以很好地完成这项工作:

// Our initial guess is that `total[0]` is the maximum sales total.
int maxSales = total[0];
int maxI = 0;
for (int i = 1; i < total.length; ++i) {
    if (totals[i] > maxSales) {
        maxSales = total[i];
        maxI = i;
    }
}
System.out.println(locations[maxI] + " has the most sales: " + maxSales);

Maybe try adding this to the end of the code.也许尝试将其添加到代码的末尾。

int max = 0; // stores the maximum value
for (int i = 0; i < total.length; i++) {
    max = Math.max(max, total[i]);
}
System.out.println(max);

This should print the greatest value of the total array.这应该打印整个数组的最大值。 I like your code!我喜欢你的代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM