如何在我的二维数组中显示最大金额?

huangapple go评论69阅读模式
英文:

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 maxTotalIndex = 0;
    for (int i = 1; i < total.length; i++) {
      if (total[i] > total[maxTotalIndex]) {
        maxTotalIndex = i;
      }
    }
    System.out.println("Location with most sold licenses: " + locations[maxTotalIndex]);
  }
}
英文:
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&lt;String&gt; unpopularSoftware = new ArrayList&lt;String&gt;();

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

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

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

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

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]); under System.out.println(&quot;\t| &quot; + 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]); 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; 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.

答案1

得分: 2

如果您想要获得总销售额最高的城市名称,您需要在total数组中查找最大的总销售额,并在此过程中跟踪到目前为止看到的最大总销售额以及您看到该最大总销售额的索引。

可以使用类似以下的代码完成这项任务:

// 我们的初始猜测是`total[0]`是最大销售总额。
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] + "拥有最高销售额:" + maxSales);
英文:

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.

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 &lt; total.length; ++i) {
    if (totals[i] &gt; maxSales) {
        maxSales = total[i];
        maxI = i;
    }
}
System.out.println(locations[maxI] + &quot; has the most sales: &quot; + maxSales);

答案2

得分: 1

也许尝试将以下代码添加到代码末尾
```java
int max = 0; // 存储最大值
for (int i = 0; i < total.length; i++) {
    max = Math.max(max, total[i]);
}
System.out.println(max);

这将打印出 total 数组中的最大值。我喜欢你的代码!


<details>
<summary>英文:</summary>
Maybe try adding this to the end of the code.
```java
int max = 0; // stores the maximum value
for (int i = 0; i &lt; 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!

huangapple
  • 本文由 发表于 2020年4月10日 10:19:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61133080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定