-
looping a sentinel controlled loop...
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!");
-
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks