Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

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

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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?

    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);
     
    }
    }
    Last edited by JavaPF; April 28th, 2009 at 01:57 AM. Reason: Please use code tags...

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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

  7. #7
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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?

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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

  9. #9
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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?

    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);
            }
     
    }

  10. #10
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Would You Use Loops and If/Else for This?

    You might say it's a bit "hacky" lol.
    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

  11. The Following User Says Thank You to Freaky Chris For This Useful Post:

    napenthia (April 29th, 2009)

  12. #11
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by napenthia; April 29th, 2009 at 02:36 AM.

  13. #12
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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

Similar Threads

  1. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 02:15 AM