help please. problems with while and switch statements.

loki556's Avatar, Join Date: Oct 2010
Newbie 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 shabbir; 18Oct2010 at 08:31.. Reason: Code blocks
0
loki556's Avatar, Join Date: Oct 2010
Newbie Member
the program*
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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");
}
}