Click to See Complete Forum and Search --> : looping a sentinel controlled loop...


kev269
12-03-2004, 11:55 AM
Hi,

The following code loops the InputDialog box and adds the entered numbers to the 'total' variable and displays the total at the end. I want to loop the current loop so that once its broken up (by entering -1), i want it to run the current loop over again (asking to input a number whilst keep adding to the total). I want to be done repeatedly until i type in a number so that it breaks from both loops. im not sure what i have to do to do this and im really new to java. Thanks a lot in advance.

final int SENTINEL = -1

while (latestNum != SENTINEL)
{
if (latestNum <= 6 )
System.out.println(latestNum + " Runs Scored!");
else
JOptionPane.showMessageDialog(null,"Please Enter Number Between 1 - 6");

total = total + latestNum;
str = JOptionPane.showInputDialog("Next Points?");

latestNum = Integer.parseInt(str);
}

System.out.println("He Is Out!");

Philwx
12-04-2004, 04:53 PM
I tested your code and it seems to work well in general (excluding the missing variable declarations).

It leaves the loop perfectly if someone enters a -1. I think that we don't know what you want out of it? What it is doing sounds reasonable. What you want it to do is hard to decipher. If you rephrase it a little it might help.