Help with Java programming "For" loop

325i

Senior Member
Joined
Jan 1, 2011
Messages
553
Reaction score
27
Location
Durbs
Hi I need some help with Java....i am using jGrasp. And i need a solution to this problem using the "for" loop.

It says:

1.1 input 5 numbers
1.2 Determine and output the largest number
1.3 Determine and output the sum of the numbers
1.4 Determine and output the average of the numbers

using simple java and scanner class....help would be greatly appreciated...:D:D

so far this is what i have...

import java.util.*;
public class Calculations
{
public static void main (String []args)
{
Scanner kb = new Scanner (System.in);
int largest=0;
int number=0;
int sum=0;
for (int i=1; i<=5; i++)
{
System.out.println("Enter number"+i);
number = kb.nextInt();
sum =number+i ;
double average = sum/5;
System.out.println("Sum "+sum);
System.out.println("Average "+average);
}
}
}
 
First, don't declare "average" in your loop, declare it with the rest of your variables. Secondly, your "sum" formula is wrong. It should be:

sum = sum+number;

Number + I will just set sum to the number entered, + the current index of the loop. Probably not what you need. Other that that, it should work.
 
thanks Sysem.........if only our I.T teacher could teach....*sigh* thanks for great tip :D
 
I'll give you a high level solution. This looks like homework :P

What I would do is read all the numbers into an Array or just make 5 vars (this can be done in one for loop that runs 5 times.) Then in the second for loop you start with a var and compare that to "largest", followed by adding that var to sum (If you use an array that would be best). Close for loop, then Display largest, display sum, display sum/5

so first for loop:

for(How many numbers)
{
Read number;
add number to array or spesified var;


}

/*second*/ for(how many numbers)
{
if(largest<currentVar or Array)
largest = currentVar or Array

sum += currentVar or Array;

}
display largest;
display sum;
display sum /5;


Functions like average cant be in loops, also displaying the stats in the loop not the best option unless you want to see how the computer runs step by step (good for debugging not so good when presenting it to you teachers)
I would suggest using an array for this assignment, score some extra marks, plus its easier and better to code like that once you get the hang of it :) If you have already used arrays in class then using 5 vars would be considered wrong
 
Using an array would definantly solve the problems. Might be a tough task if OP hasn't learnt any of that yet. A simplier way would be to assign the largest number to 0, and then check if the number entered is bigger that the largest. If so, assign the largest to the new number.

Ie:
Largest = 0;
Loop
{
If Largest < number
Largest = number
}

Then display the sum, largest and average as Little Mermaid suggested.
 
:wtf::eek::erm:......okay this is homework.......but we dont get taught How to do it...we're just told.."Okay see if can get up to page 103 before exams."
 
When i was in school the tip they gave us for a situation like this (Getting the highest and lowest number), is that when you read in the first number, set the highest and lowest to that value. then every number after that, compare to the highest and lowest variable, and change if necessary.

Hope that made sense...:o
 
erm :erm: no it never.....this is only my 1st year doing I.T programming
 
Yo 321i,
Welcome to coding :P

Another exercise on the same problem..
1. n-1 input numbers : hint wait for ESC key /* only loop here */
2. display results /* no loops */

there are many ways to skin a cat in coding

Enjoy,
Igs
 
thanks IGS.....i kinda left that one out...:D Our teacher doesn't want any skinning.....otherwise he said he'll skin us...lol btw its "325i"....:D
 
Top
Sign up to the MyBroadband newsletter
X