Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   help please. problems with while and switch statements. (http://www.go4expert.com/forums/help-switch-statements-t23602/)

loki556 18Oct2010 05:41

help please. problems with while and switch statements.
 
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.

loki556 18Oct2010 05:47

Re: help please. problems with while and switch statements.
 
the program*

virxen 29Oct2010 03:06

Re: help please. problems with while and switch statements.
 
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");
}
}



All times are GMT +5.5. The time now is 08:03.