A small program in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • this_n00b_rocks
    Banned
    • Aug 2006
    • 510

    #1

    A small program in java

    I need to make a program to take the tens and ones digits of a number, and display them. The only problem is... I don't know how! Can someone help me?
  • RainGame53
    FFR Player
    • May 2005
    • 61

    #2
    Re: A small program in java

    //myNumber is the number to be checked
    int onesDigit = myNumber % 10;
    int tensDigit = (myNumber - onesDigit) / 10;

    System.out.println("Ones Digit - " + onesDigit + ", Tens Digit - " + tensDigit);

    Comment

    • this_n00b_rocks
      Banned
      • Aug 2006
      • 510

      #3
      Re: A small program in java

      Sweet, I'll try it out.

      Comment

      • RainGame53
        FFR Player
        • May 2005
        • 61

        #4
        Re: A small program in java

        No problem. Note that that won't work for any numbers 100+ though, if you need that as well, let me know.

        Comment

        • Afrobean
          Admiral in the Red Army
          • Dec 2003
          • 13262

          #5
          Re: A small program in java

          Isn't there a simple way to round numbers? I'm not entirely familiar with Java, but logic would tell me that there would be a math operator that would round to the number of places you tell it to.

          EDIT: yeah, there definitely is. http://www.cafeaulait.org/course/week4/40.html
          Code:
          // Truncating and Rounding functions
           
              // You can round off a floating point number  
              // to the nearest integer with round()
               System.out.println(x + " is approximately " + Math.round(x));     
               System.out.println(y + " is approximately " + Math.round(y));
          working on finding out exactly how to work it

          EDIT*2: looks like you'll have to use round and multiplication and division to get it how you want it exactly
          Last edited by Afrobean; 10-2-2006, 03:22 PM.

          Comment

          • RainGame53
            FFR Player
            • May 2005
            • 61

            #6
            Re: A small program in java

            that gets rid of the decimal; I don't think it will help in getting the digits though

            maybe I'm wrong

            Comment

            • Afrobean
              Admiral in the Red Army
              • Dec 2003
              • 13262

              #7
              Re: A small program in java

              ok look at this:

              Code:
              double PENIS = 123.45;
              double vagina;
              
              vagina = PENIS * 0.01; // vagina is now 1.2345
              
              vagina = round(vagina); //vagina now equals 1
              
              vagina = vagina * 100; //vagina now equals 100
              Number was originally 123.45. After all of the math, the number becomes 100. That would be 123.45 rounded to the hundreds place.

              It can be done that way.

              Comment

              • Tasselfoot
                Retired BOSS
                FFR Simfile Author
                • Jul 2003
                • 25185

                #8
                Re: A small program in java

                this really is about as easy of a problem as you could expect from an intro to java type class.....


                no offense, but while we are here to help, we are NOT here to do your homework for you. especially on programs that aren't even remotely difficult.
                RIP

                Comment

                • this_n00b_rocks
                  Banned
                  • Aug 2006
                  • 510

                  #9
                  Re: A small program in java

                  I made my own code for it, and it worked. I just wanted to find another way to code it, for future reference.

                  Comment

                  • Afrobean
                    Admiral in the Red Army
                    • Dec 2003
                    • 13262

                    #10
                    Re: A small program in java

                    Originally posted by this_n00b_rocks
                    I made my own code for it, and it worked. I just wanted to find another way to code it, for future reference.
                    Post it. I'm curious to see how you did it.

                    Comment

                    • Codename46
                      FFR Player
                      • Sep 2006
                      • 108

                      #11
                      Re: A small program in java

                      Originally posted by Afrobean
                      ok look at this:

                      Code:
                      double PENIS = 123.45;
                      double vagina;
                      
                      vagina = PENIS * 0.01; // vagina is now 1.2345
                      
                      vagina = round(vagina); //vagina now equals 1
                      
                      vagina = vagina * 100; //vagina now equals 100
                      Number was originally 123.45. After all of the math, the number becomes 100. That would be 123.45 rounded to the hundreds place.

                      It can be done that way.
                      Or I think you can use DecimalFormat as well to round to a certain number of digits.

                      Comment

                      • Afrobean
                        Admiral in the Red Army
                        • Dec 2003
                        • 13262

                        #12
                        Re: A small program in java

                        Originally posted by Codename46
                        Or I think you can use DecimalFormat as well to round to a certain number of digits.
                        Yeah, format wouldn't actually round the number though. What I put would round the number and store it as a new variable. Formatting it would simply format the number for output.

                        Unless I'm mistaken about the function of formatting. Doesn't it work with strings?

                        Comment

                        • this_n00b_rocks
                          Banned
                          • Aug 2006
                          • 510

                          #13
                          Re: A small program in java

                          import java.util.*;
                          /*
                          * Michael Cuiffi
                          * Period 8
                          * 10/2/06
                          */

                          public class Digits {

                          public static void main(String[] args) {
                          int userNumber; //User number
                          int tens; //The number in the tens digit
                          int ones; //The number in the ones digit
                          Scanner input = new Scanner(System.in);

                          System.out.print("Enter any two-digit integer: ");
                          userNumber = input.nextInt();
                          input.close();
                          //myNumber is the number to be checked
                          tens = (int)userNumber / 10;
                          ones = userNumber - (tens * 10);
                          System.out.println("Tens Digit - " + tens);
                          System.out.println("Ones Digit - " + ones);
                          }
                          }

                          Comment

                          Working...