Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help with do loop.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Kenster102.5




PostPosted: Tue Nov 11, 2008 7:22 pm   Post subject: Need help with do loop.

I am creating this program for school board selection which uses a do loop. The user must choose either Catholic school board which is represented by "C" or Public school board which is represented by "P". The users is prompted within the do loop, and the users entry is stored as type character. A case will be used to tell the user their selection. The loop will constantly run until if they enter the incorrect information, but should stop once "C" or "P" is entered, but it doesn't and continues to ask the user.

Written with Ready to Program Java IDE
code:


char schoolBoard;
do
        {
//Prompts user.           
c.println ("Please enter your school board of your choice below. \nEnter \"P\" for Public school board, or enter \"C\" for Catholic schoolboard below");

//Reads in information and stores users input.
            schoolBoard = c.readChar ();
           
            switch (schoolBoard)
            {
                case 'P':
                    c.println ("You have chosen Public board as your school board");
                    break;
                case 'C':
                    c.println ("You have chosen Catholic board as your school board");
                    break;
                   
            }
        }
        while (schoolBoard != 'P' || schoolBoard != 'C');

Can I use OR operator, or do I have to create another do loop for the second character?
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Tue Nov 11, 2008 7:36 pm   Post subject: Re: Need help with do loop.

You need AND:
Java:
while (schoolBoard != 'P' && schoolBoard != 'C')

Otherwise you won't be able to exit the loop unless schoolBoard is equal to both P and C at the same time.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: