Hi

I have this problem where I tell the user to prompt 10 integers separated with spaces. And then I have to output the same numbers the user entered and then output max and min numbers. So How can I get the maximum value of these multiple integers?

This is my code:

System.out.println("Enter ten integers separated with spaces:");
        int num1 = scan.nextInt();
        int num2 = scan.nextInt();
        int num3 = scan.nextInt();
        int num4 = scan.nextInt();
        int num5 = scan.nextInt();
        int num6 = scan.nextInt();
        int num7 = scan.nextInt();
        int num8 = scan.nextInt();
        int num9 = scan.nextInt();
        int num10 = scan.nextInt();
        int Sum = num1+num2+num3+num4+num5+num6+num7+num8+num9+num10;
        int Average = (Sum/10);
        int counter = 0;
        System.out.println("The numbers you entered are: ");
        while(counter<10){
        System.out.print("The numbers you entered are:\n"+num1+" "+num2+" "+num3+" "+num4+" "+num5+" "+num6+" "+num7+" "+num8+" "+num9+" "+num10);
        counter++;
        }
        }
    }

I have to use while loop and/or do-while loop to solve it. So don't tell me to use for loop. :)

Any help?:confused:

Thanks.

Dani AI

Generated

The symptom that saw (only the last number printing) comes from consuming all input with repeated calls to nextInt() without storing each value. A later nextInt() does not "replay" earlier tokens; it reads the next token (or blocks). The simplest, robust pattern is: read each integer once, store it (an array is ideal), and update sum, max and min inside the same while loop. This follows 's suggestion to update max in the input loop and 's suggestion to store values for later printing.

A practical, while-loop-only pattern:

  • allocate an int[] of length 10 and an index variable,
  • initialize sum and set max to Integer.MIN_VALUE and min to Integer.MAX_VALUE (or seed both with the first input),
  • in a while (index < 10) read a value, save it to the array, add to sum, and update max/min,
  • afterwards iterate the array with a while loop to print values and compute the average using floating-point division.

Example (keeps everything in two while loops and guards bad tokens):

Scanner input = new Scanner(System.in);
int[] values = new int[10];
int i = 0;
int sum = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

System.out.println("Enter ten integers separated with spaces:");
while (i < 10) {
    if (!input.hasNextInt()) {
        input.next(); // skip invalid token
        continue;
    }
    int val = input.nextInt();
    values[i++] = val;
    sum += val;
    if (val > max) max = val;
    if (val < min) min = val;
}

System.out.print("The integers you entered are:");
i = 0;
while (i < 10) {
    System.out.print(" " + values[i++]);
}
System.out.println();
System.out.println("Sum = " + sum);
System.out.println("Average = " + (sum / 10.0));
System.out.println("Max = " + max);
System.out.println("Min = " + min);

Notes: do not initialize max to 0 (fails for all-negative input). Use hasNextInt() to handle bad tokens. For very large inputs, use a long for the running sum to avoid overflow.

Recommended Answers

All 3 Replies

Try using a while loop to calculate the sum and the maximum

int counter = 0
int sum =0;
while(counter <10)
{
 sum += scan.nextInt();
}

In that same loop try to figure out how to get the maximum of the number. Like this:

if( nextInt > max)
max = nextInt

Hope it helps, try it out and post back.

I removed the declarations of integers and wrote this code:

Scanner input = new Scanner(System.in);
        System.out.println("Enter ten integers separated with spaces:");
        int numbers = 0;
        int Sum = 0;
        int counter = 0;
        int Max = 0;
        while(counter<10)
        {
            Sum += input.nextInt();
            counter++;
        }
        numbers = input.nextInt();
        float Average = ((float)Sum/10);
        
        System.out.println("The integers you entered are: " + numbers);
        
        
            System.out.print("Sum = " + Sum);
        }
}

Now the Sum worked fine. But when the compiler prints the numbers that the user entered, it outputs only the last number.
Also I tried to write the max code that you gave me but the compiler is not recognizing nextInt.

Thanks.

If you want to print the numbers the user entered after they're done entering them, they you'll have to store the numbers somewhere. Students most commonly do this with an array.

I think that your sum and average logic are OK. But if you test your program carefully, you'll probably find that the "last number" that it's printing is not included in the sum or average. (But if you switch to arrays, you might end up fixing this problem as a side-effect, while making other changes.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.