A small program in java

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

    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?
  • #2
    FFR Player
    RainGame53
    • May 2005
    • 61

    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

    • #3
      Banned
      this_n00b_rocks
      • Aug 2006
      • 510

      Re: A small program in java

      Sweet, I'll try it out.

      Comment

      • #4
        FFR Player
        RainGame53
        • May 2005
        • 61

        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

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

          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, 04:22 PM.

          Comment

          • #6
            FFR Player
            RainGame53
            • May 2005
            • 61

            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

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

              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

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

                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

                • #9
                  Banned
                  this_n00b_rocks
                  • Aug 2006
                  • 510

                  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

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

                    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

                    • #11
                      FFR Player
                      Codename46
                      • Sep 2006
                      • 108

                      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

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

                        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

                        • #13
                          Banned
                          this_n00b_rocks
                          • Aug 2006
                          • 510

                          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...