# Would i use loop or if/else which reads a list of positive integers and calculate their average?

Printable View

• April 27th, 2009, 12:17 PM
napenthia
Would i use loop or if/else which reads a list of positive integers and calculate their average?
Design and implement an application that reads a list of positive integers values and calculate their average. List terminates with -1.

What I would like to know is would I be using the if/else conditions for something like this?
Would I implement a loop for this? (although im pretty sure i wont be)
If I am putting in more then one number for the program, Do I have to set up more then one input for the scanner or can I put it on a loop for a set number of times?
• April 27th, 2009, 12:28 PM
Freaky Chris
Re: Would You Use Loops and If/Else for This?
You can do it in a number of ways, but for sure you will need loops.
Also you will need an if statement to check if the input is a -1. You can write this in one loop, and it would only be about 5 lines :). I'll post you my solution if you want at some point.

Chris
• April 27th, 2009, 05:37 PM
napenthia
Re: Would You Use Loops and If/Else for This?
ok cool, im gonna try it out and ill post what i have for you to check but what kind of loop should i be using? I thought maybe a do while loop but im not sure
• April 27th, 2009, 05:41 PM
Freaky Chris
Re: Would You Use Loops and If/Else for This?
Do while would certainly be better than a while loop in this case. I myself used a for loop.

Chris
• April 27th, 2009, 10:50 PM
napenthia
Re: Would You Use Loops and If/Else for This?
ok this is what i have so far but i am a little confused as how to incorporate the if statement in here, should i change anything?

Code :

```import java.util.Scanner;   public class Main {   public static void main(String[] args) {   Scanner scan = new Scanner(System.in); int i;   do { System.out.println("Enter a number:"); i = scan.nextInt();   }while(i > -1);   } }```
• April 28th, 2009, 01:41 AM
Freaky Chris
Re: Would You Use Loops and If/Else for This?
if you change, while(i> -1) to while(i != -1) then you don't need the if statement.

Chris
• April 28th, 2009, 11:07 AM
napenthia
Re: Would You Use Loops and If/Else for This?
ok so i change that, but i still need to find a way to calculate the average, would i still need the if statement then?
• April 28th, 2009, 11:09 AM
Freaky Chris
Re: Would You Use Loops and If/Else for This?
Just add all the number up that the user inputs, then divide them by the number of numbers they entered after your while loop.

Chris
• April 28th, 2009, 08:10 PM
napenthia
Re: Would You Use Loops and If/Else for This?
ok i used the while loop with a sentinel value, and put the average at the end like you suggested here is what i got. would you mind posting the way you did it so that i can compare, and so that i will have an example of the way you did it with the for loop?

Code :

```import java.util.Scanner;   public class Main {   public static void main(String[] args) {   Scanner scan = new Scanner(System.in); int number = 0; int count = 0; int sum = 0; double average;       while (number != -1) { count++; sum += number; System.out.println("Enter a number:"); number = scan.nextInt();   }   average = (double)sum/count;   System.out.println("The average is: " + average); }   }```
• April 29th, 2009, 01:40 AM
Freaky Chris
Re: Would You Use Loops and If/Else for This?
You might say it's a bit "hacky" lol.
Code :

```import java.util.Scanner;   public class Average { public static void main(String[] args) { int total = 0, i = 0; for(int temp; (temp = new Scanner(System.in).nextInt()) != -1; total+= temp, i++); System.out.println("Average: " + (double)total/i); } }```
Regards,
Chris
• April 29th, 2009, 02:14 AM
napenthia
Re: Would You Use Loops and If/Else for This?
Thanks for posting it, I get how to use now, well sorta. But when doing the average, why is it that you have to put the (double) like that, I saw it in my book but it doesnt explain why.
On another note though, when input a set of numbers like 8986 how do you get them to output like this
8 9 8 6?
Would I also use a loop for this?

design and implement an application that prompts the user to input an integer and then outputs both the individual digits and the sum of the digits.
• April 29th, 2009, 03:12 AM
Freaky Chris
Re: Would You Use Loops and If/Else for This?
Because I used integer division, i.e 5/2 the result would have been 2. Rather than 2.5 using (double) is a cast, it casts the number to a double thus it can then output the real value of 2.5

You next problem sounds like something you need to think about. If the uses eneters one number,
345 you need to split it into indervidual digits. My cluse to you is a bit of Math using the / (division) and % (modulus) operator.

Regards,
Chris