looping a sentinel controlled loop...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: looping a sentinel controlled loop...

  1. #1
    Join Date
    Dec 2004
    Posts
    1

    Angry 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!");

  2. #2
    Join Date
    Sep 2004
    Posts
    150
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center