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

Username:   Password: 
 RegisterRegister   
 What type of loop should I use
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Williamarf




PostPosted: Fri Oct 19, 2012 4:10 pm   Post subject: What type of loop should I use

I made this program for an assignment and its finnished, but I want to add a loop so I don't have to rerun my programe for each char. What type would be best?

Thanks for any assistance you can provide. Canada


Java:

/***********************************************************
* Name: William Frisk              Date: October 11, 2012  *
*                                                          *
* Description: This simple program will determain weather  *
*              the user has typed in a digit or a letter,  *
*              if its a letter it will detect whether it   *
*              is uppercase, lowercase, or a consonant.    *
***********************************************************/


   import java.util.Scanner; //importing the scanner

   class Letter_Diget
   {
      public static void main (String[] args)
      {
         Scanner sc = new Scanner(System.in);
     
         System.out.print("Enter a Character: ");
         char ch = sc.nextLine().charAt(0); // take only the first char
     
         if((ch >= '0') && (ch <= '9'))// Check if user typed a number
            System.out.println("It is a diget");
           
         //Check if user typed an UPPERCASE letter     
         else if ((ch >= 'A') && (ch <= 'Z'))
         
            if ((ch != 'A') && (ch != 'E') && (ch != 'I') && (ch != 'O') &&(ch != 'U'))
               System.out.println("It is a letter, uppercase, consonant.");// If its a consonant AND uppercase
               
            else
               System.out.println("It is a letter, uppercase, vowel.");// If its a vowel AND uppercase
         
         
         //Check if user typed a LOWERCASE letter   
         else if ((ch >= 'a') && (ch <= 'z'))
         
            if ((ch != 'a') && (ch != 'e') && (ch != 'i') && (ch != 'o') &&(ch != 'u'))
               System.out.println("It is a letter, lowercase, consonant.");//if its a consonant AND lowercase
               
            else
               System.out.println("It is a letter, lowercase, vowel.");//if its a vowel AND lowercase
         
         else
            System.out.println("It is a punctuation mark.");
     
      }
   }
Sponsor
Sponsor
Sponsor
sponsor
whoareyou




PostPosted: Fri Oct 19, 2012 6:10 pm   Post subject: RE:What type of loop should I use

I think a do-while loop would be appropriate.
QuantumPhysics




PostPosted: Sat Oct 20, 2012 2:03 am   Post subject: Re: What type of loop should I use

Or you can have a for loop and have the user input the amount of times to run the program

Java:

//scanner....

runTimes = scannerName.nextInt();
for(int i=0;i<=runTimes;++i){
//code
}


And, it is actually spelled - 'Digit' not 'Diget' (That sounds more like dig it)
Insectoid




PostPosted: Sat Oct 20, 2012 6:02 am   Post subject: RE:What type of loop should I use

Or you could use a for loop with a boolean.

code:
for (boolean continue=false; !continue;)
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  [ 4 Posts ]
Jump to:   


Style:  
Search: