9 Replies - 1296 Views - Last Post: 07 March 2012 - 08:37 PM Rate Topic: -----

#1 marcb1387   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

sentinel while loop

Posted 07 March 2012 - 07:26 PM

import java.util.*;

public class Average {
  public static void main(String[] args) {
   Scanner console = new Scanner(System.in);
   int next = 0; 
   int total = 0; 
   int counter = 0;
   
   do {
	   counter++ ;
	   System.out.println("Type a Number: ");
	   next = console.nextInt();
	   total = next + total ;

   }
   while(next >= 0);
  System.out.println("average is " + (total / counter));
  }
}



trying to use a while loop to add x amount of integers but when a negative number is inputted the loop stops and prints the average of the numbers that you have entered

Is This A Good Question/Topic? 0
  • +

Replies To: sentinel while loop

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: sentinel while loop

Posted 07 March 2012 - 07:31 PM

Okay. What is the question?
Was This Post Helpful? 0
  • +
  • -

#3 jdavi134   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: sentinel while loop

Posted 07 March 2012 - 07:34 PM

Well look at your while loop. It tells the loop to stop when you enter a negative. Wouldn't changing that help? Try using a char set to 'y' or 'n'.

'y' would be if you wanted to keep going.
'n' would be if you didn't

Also. Like what Atli said before me. Ask a question. Don't just say what you want the program to do.

Jack
Was This Post Helpful? 0
  • +
  • -

#4 marcb1387   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

Re: sentinel while loop

Posted 07 March 2012 - 07:51 PM

when i run the program it seems to be adding the negative number in before stopping the loop

and my averaging is off somehow im not sure how but if i enter 4, 4 then -1 it comes up with an average of 2 instead of 4 and i have no idea what i entered wrong

i want the negative number to be my sentinel to stop the loop and not add it in
Was This Post Helpful? 0
  • +
  • -

#5 jdavi134   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: sentinel while loop

Posted 07 March 2012 - 07:52 PM

You could add an if statement that would check to see if the number was positive before adding it to the total. If it wasn't the loop would end right then. Also, don't increment your counter unless the number that you have entered is positive. So you would have to include it in the if statement.

Jack
Was This Post Helpful? 0
  • +
  • -

#6 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: sentinel while loop

Posted 07 March 2012 - 07:58 PM

Consider your logic:
do {
	Read in a number.
	Add it to the total.
	Increment the counter.
}
while (The last number was not negative)

Print the average



Now step through it. Assuming you enter 4, 4 and -1:
Read in 4
Add 4 to the total, which is now 4.
Increment counter to 1

Read in 4
Add 4 to the total, which is now 8.
Incremenet counter to 2

Read in -1
Add -1 to the total, which is now 7
Increment counter to 3.

End the do...while loop, since you read in -1 last.

Calculate average: 7 / 3 == 2.333
Print average as an integer, which is rounded to 2.


Do you see the problem?
Was This Post Helpful? 0
  • +
  • -

#7 marcb1387   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

Re: sentinel while loop

Posted 07 March 2012 - 08:09 PM

i do and cannot figure out how to perform the check before it adds it to total i have also tried it this way

import java.util.*;

public class Average {
  public static void main(String[] args) {
   Scanner console = new Scanner(System.in);
   int next = 0; 
   int total = 0; 
   int counter = 0;
   
   while(next >= 0) {
	   counter++ ;
	   System.out.println("Type a Number: ");
	   next = console.nextInt();
	   total = next + total ;

   }
  System.out.println("average is " + (total / counter));
  }
}


Was This Post Helpful? 0
  • +
  • -

#8 jdavi134   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: sentinel while loop

Posted 07 March 2012 - 08:24 PM

Well think about it. If you want to check whether or not the number is positive you should use an if statement that would allow you to do this. The format would look something like this


do{
           System.out.println("Enter a number: ");
           next = console.nextInt();
           
           if(??)
           {
                //add in the statement to increment the counter , and to add the number entered.
           }

}while(next >= 0);


This post has been edited by jdavi134: 07 March 2012 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#9 marcb1387   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

Re: sentinel while loop

Posted 07 March 2012 - 08:29 PM

i see thank you very much
Was This Post Helpful? 0
  • +
  • -

#10 JavaSuperNoob   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 59
  • Joined: 13-February 12

Re: sentinel while loop

Posted 07 March 2012 - 08:37 PM

Here is another way you could do it if you are interested to learn. I have commented on the sections


import java.util.*;

public class Average {

    public static void main(String[] args) {
        Scanner sin = new Scanner(System.in);
        int nNum;
        int nAccum = 0;
        int nAdd = 0;
        int nFinal;
        while (true) {
            System.out.println("Enter a number");
            nNum = sin.nextInt();
            if (nNum > 0) {//if num is greater than 0 it will add and accumulator will increment
                nAccum += 1;
                nAdd += nNum;
            }
            if (nNum < 0) {
                nFinal = nAdd / nAccum;//if it is negetive it will do following
                System.out.println(nFinal);
                break;

            }
        }


    }
}

This post has been edited by Atli: 07 March 2012 - 08:40 PM
Reason for edit:: Please use [code] tags when posting code.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1