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?
A small program in java
Collapse
X
-
Tags: None
-
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); -
-
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
-
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
working on finding out exactly how to work itCode:// 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));
EDIT*2: looks like you'll have to use round and multiplication and division to get it how you want it exactlyLast edited by Afrobean; 10-2-2006, 03:22 PM.
Comment
-
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 wrongComment
-
Re: A small program in java
ok look at this:
Number was originally 123.45. After all of the math, the number becomes 100. That would be 123.45 rounded to the hundreds place.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
It can be done that way.
Comment
-
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.RIPComment
-
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
-
Re: A small program in java
Or I think you can use DecimalFormat as well to round to a certain number of digits.ok look at this:
Number was originally 123.45. After all of the math, the number becomes 100. That would be 123.45 rounded to the hundreds place.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
It can be done that way.Comment
-
Re: A small program in java
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
-
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
Comment