Results 1 to 3 of 3
  1. #1
    trill is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with gigantamous switch statement

    Hi, I have a question with some code. I am making a simple cheesy little program that changes any int that a user inputs into that numbers binary value. Here is a sample of my code:

    Java Code:
    int number
    
    System.out.print("Type an int from 1 to 99: ");
    number = InputOutput.getInt();
    
    switch (number)
    {
            case 1: System.out.println("10000000");
                    break;
            case 2: System.out.println("01000000");
                    break;
            case 3: System.out.println("11000000");
                    break;
    Is there any way to make this code easier or should i say shorter to write instead of one gigantamous switch statement? and one last thing, if i asked the user if they wanted to type another int, and they said yes, how would i loop the program?
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Is there any way to make this code easier or should i say shorter to write instead of one gigantamous switch statement?
    The way you have it looks okay. You could try the convenience method shown below.
    how would i loop the program
    Try this (pseudocode):
    Java Code:
    do {
        System.out.print("Type an int from 1 to 99: ");
        int number = InputOutput.getInt();
        String binary = Integer.toBinaryString(number);
        System.out.println("binary = " + binary);
        System.out.println("Want more? \"y\"  \"n\"");
        String s = InputOutput.readLine();
    } while(s.equals("y"));

  3. #3
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Just found out that there was a toBinaryString(), thanks hardwired ;)
    Btw, this statement
    Java Code:
    String s = InputOutput.readLine();
    U two used it, is there exist an InputOutput class or is it just ur custom created one?:confused:

Similar Threads

  1. Method in a Switch Statement
    By cart1443 in forum New To Java
    Replies: 6
    Last Post: 03-14-2008, 03:48 AM
  2. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM
  3. Replies: 0
    Last Post: 01-04-2008, 09:32 AM
  4. Problem with a switch statement in Java
    By baltimore in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 04:43 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •