Getting the sum of an array in steps of 10 JAVA

Explanation

I was given a task in the class "Computer Science" class 12, which completely puzzled me. So far I have tried to use a bunch of if statements (although I know this is not the best way to do this, although it will probably work), but it did not work. Then I tried using nested loops, but that didn't work, here is the code that I have created so far for this method.

Code (Note: Max = 1000)

private static void totals2 (Scanner user_input,int max){
        int[] num = new int[max];
        int[] total=new int[10];
        System.out.println("Welcome to totals\nThis method gathers input and displays the sum depending on which category they fall in");
        System.out.println("Enter numbers from 0-99 only (Up to 1000 inputs)\nEnter any number outside of 0-99 to stop collecting\n");
        for (int i = 0; i < num.length; i++) {
            System.out.println("Please enter a number to be stored in index: " + i);
            num[i] = user_input.nextInt();
            if (num[i] < 0 || num[i] > 99) {
                break;
            }
        }
        for(int i = 0; i < 100; i +=10){
            int j;
            int k=(i/10)-1;
            for(j=0 ;j<1000;j++){
                if (num[j] <= i){
                    total[k]+= num[j];
                }
            }
           System.out.println(total[k]);
        }
    }

Question

Create a procedure that will display the total number of all numbers (in the array) entered less than 10, the total number of all entered numbers less than 20, the total number of all entered numbers less than 30, ... and the total number of all entered numbers less than 100 (therefore 88 are included in both totals for numbers less than 90 and less than 100).

+4
3

, . .

  • 0 99? , .

  • , = 0, k? = 10, k?

, , , . , , . ( Intellj Java)

+1

-, , (- 1, (0)), , ArrayIndexOutOfBoundsException. 20 , 0 19.... 0 999. 1000 num.length, , ....

, 10 ", , i 10 0. ... ... , , , 0 10, 1 [(0/10) - 1]? k , , ArrayIndexOutOfBoundsException.

, , , , 10, , 20, , 30.... , 100. (... < =...). if .

:

, totals2(). " ".

0

Source: https://habr.com/ru/post/1686084/


All Articles