英文:
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<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]);
under 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]);
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 < total.length; ++i) {
if (totals[i] > maxSales) {
maxSales = total[i];
maxI = i;
}
}
System.out.println(locations[maxI] + " has the most sales: " + 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 < 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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论