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.

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.