数组的最小值返回0

时间:2021-10-19 01:43:16

I'm having problems figuring out why returns for my minimum value in my array keep ending up as 0. I've checked several questions with the same problem but can't use those solutions because my array is being created in one method and my min/max values are calculated in another method.

我有问题弄清楚为什么我的数组中的最小值的返回值最终为0.我已经检查了几个问题同样的问题但是不能使用这些解决方案,因为我的数组是在一个方法中创建的,而我的最小/最大值以另一种方法计算。

Is there anyway I can keep my min/max value in a separate method and still get a non-zero answer for my min value? Also, there is more code in the processSalesReport method but I left it out because it was irrelevant. Thanks ahead of time!

无论如何,我可以在一个单独的方法中保持我的最小/最大值,并仍然得到我的最小值的非零答案?此外,processSalesReport方法中有更多代码,但我将其删除,因为它无关紧要。提前谢谢!

import java.util.Arrays;
import java.util.Scanner;

public class CarSalesReport {

int sum;
int count = 0;
int[] num = new int[1500];
String ans = "";
Scanner userInput = new Scanner(System.in);

public CarSalesReport(Scanner input) {
    String regex = "\\d+|done";
    System.out.println("Type sales (type \"done\" when finished)");

    do{
        System.out.print("Sale Number " + (count + 1) +  ": ");
        ans = userInput.nextLine();
        while(!ans.matches(regex)){
            System.out.println("Please enter a positive number");
            ans = userInput.nextLine();
        }
        if(!ans.equalsIgnoreCase("done")){
            int ans1 = Integer.parseInt(ans);
            num[count] = ans1;
            count++;
        }           
    }while(!ans.equalsIgnoreCase("done"));
}

public void processSalesReport(){
    int max = num[0];
    for(int a=0;a<num.length;a++){
        if(max<num[a]){
            max=num[a];
        }
    }
    //This is where I'm having my problems.  
    int min = Integer.MAX_VALUE;
    for(int a=1;a<num.length;a++){
        if(min>num[a]){
            min=num[a];
        } 
    } 
    Arrays.sort(num);
    System.out.println("\nMaximum sale: $" + max); 
    System.out.println("\nMinimum sale: $" + min);

}
}

1 个解决方案

#1


7  

It's because you've got 1500 entries in your array, which are all initialised to 0. You're iterating through all of them trying to find the minimum, instead of just iterating through the ones you've explicitly populated.

这是因为你的数组中有1500个条目,它们都被初始化为0.你正在迭​​代所有这些条目试图找到最小值,而不是仅仅迭代你明确填充的那些条目。

In the loop where you calculate the minimum, change

在计算最小值的循环中,更改

for (int a = 1; a < num.length; a++) {

to

for (int a = 0; a < count; a++) {

so that you only look at the entries that you've populated.

这样您只能查看已填充的条目。

#1


7  

It's because you've got 1500 entries in your array, which are all initialised to 0. You're iterating through all of them trying to find the minimum, instead of just iterating through the ones you've explicitly populated.

这是因为你的数组中有1500个条目,它们都被初始化为0.你正在迭​​代所有这些条目试图找到最小值,而不是仅仅迭代你明确填充的那些条目。

In the loop where you calculate the minimum, change

在计算最小值的循环中,更改

for (int a = 1; a < num.length; a++) {

to

for (int a = 0; a < count; a++) {

so that you only look at the entries that you've populated.

这样您只能查看已填充的条目。