1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

help please. problems with while and switch statements.

Discussion in 'Java' started by loki556, Oct 18, 2010.

  1. loki556

    loki556 New Member

    this is what i have
    Code:
    import java.util.Scanner;
    import java.util.Random;
    public class Assignment4 {
    
        /* merle ledbetter
         * MWF 1255
         * assignment 4
         */
        
    
        public static void main(String[] args){
            Scanner s= new Scanner(System.in);    
            int tries= 0;
            
            
            printMenu();
            char character = s.next().charAt(0);
            while(character != 'q'){
                switch(character){
            
            case 'a':
                System.out.println("you have selected Grid of Characters.");
                System.out.println("What is number you want to generate? [0 to 500000000]");
                int number= s.nextInt();
                while((number > 500000000)||(number< 0)){
                    System.out.println("the number is not within the range 0 to 500000000 try again");
                    number= s.nextInt();
                    }
                Random generator = new Random();
                int rand = generator.nextInt(number);
                System.out.println(rand);
                while(rand != number){
                    tries++;
                     rand= generator.nextInt(number);                
                }
                System.out.println(tries);
                System.out.println(rand);
                System.out.println(number);
                break;
            
            case 'b': case'B':
                //blah blah blah balh
                // blah blah blah blah
                break;
            
            case 'c': case'C':
                // blah blah blah blah 
                // blah blah blah
            break;
            
            case'd': case'D':
                // blah blah blah
                // blah blah blah
            break;
            
            case'e': case'E':
                // blah blah blah
                // blah blah blah
            break;
            
            case'q': case'Q':
                // blah blah blah
                // blah blah blah
            break;
            default:
                System.out.println("invalid selection try again");
                break;
                }
            }
        }
    
        public static void printMenu(){
            //opening prompt
            System.out.println("Welcome to the random task machine. Please make a selection." + "\n" +"-------------------------");
            System.out.println("A. Grid of Characters");
            System.out.println("B. How Many Tries");
            System.out.println("C. Triangle of Characters");
            System.out.println("D. Even/Odd Printing");
            System.out.println("E. Palindrome Checker");
            System.out.println("Q. Quit");
    
    i cannot get the case a to work and i need to program to iterate until q is selected.
     
    Last edited by a moderator: Oct 18, 2010
  2. loki556

    loki556 New Member

    the program*
     
  3. virxen

    virxen New Member

    see the changes in the code

    Code:
    import java.util.Scanner;
    import java.util.Random;
    public class Assignment4 {
    
        /* merle ledbetter
         * MWF 1255
         * assignment 4
         */
        
    
        public static void main(String[] args){
            Scanner s= new Scanner(System.in);    
    
            char character = '1';
            while(character != 'q'){
                int tries= 0;
                printMenu();
                character=s.next().charAt(0);
                switch(character){
            
            case 'a':case 'A':
                System.out.println("you have selected Grid of Characters.");
                System.out.println("What is number you want to generate? [0 to 500000000]");
                int number= s.nextInt();
                while((number > 500000000)||(number< 0)){
                    System.out.println("the number is not within the range 0 to 500000000 try again");
                    number= s.nextInt();
                    }
                Random generator = new Random();
                int rand = -1;
                while(rand != number){
                    tries++;
                     rand= 1+generator.nextInt(number);                
                }
                System.out.println("total tries="+tries);
                System.out.println("rand="+rand);
                System.out.println("number="+number);
                break;
            
            case 'b': case'B':
                //blah blah blah balh
                // blah blah blah blah
                break;
            
            case 'c': case'C':
                // blah blah blah blah 
                // blah blah blah
            break;
            
            case'd': case'D':
                // blah blah blah
                // blah blah blah
            break;
            
            case'e': case'E':
                // blah blah blah
                // blah blah blah
            break;
            
            case'q': case'Q':
                // blah blah blah
                // blah blah blah
            break;
            default:
                System.out.println("invalid selection try again");
                break;
                }
            }
        }
    
        public static void printMenu(){
            //opening prompt
            System.out.println("Welcome to the random task machine. Please make a selection." + "\n" +"-------------------------");
            System.out.println("A. Grid of Characters");
            System.out.println("B. How Many Tries");
            System.out.println("C. Triangle of Characters");
            System.out.println("D. Even/Odd Printing");
            System.out.println("E. Palindrome Checker");
            System.out.println("Q. Quit");
    }
    }
    
    
     

Share This Page