Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2003
    Location
    portland oregon
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with java code

    hey 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:

    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;
    . . . And it goes on . . .

    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?

    thx everyone

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Yeah you could just use some of the functions built into the API

    http://java.sun.com/j2se/1.4.2/docs/...aryString(int)
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    btw, binary is counted from right to left...
    eg:
    001 = 1
    010 = 2
    011 = 3
    ........


    Jason

  • #4
    New Coder
    Join Date
    Aug 2003
    Location
    portland oregon
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thx all, appreciate the help


  •  

    Posting Permissions

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