(defn print-digits [n]
(let [num-str (str n)]
(println "The hundreds digit is: " (nth num-str 0 "0"))
(println "The tens digit is: " (nth num-str 1 "0"))
(println "The ones digit is: " (nth num-str 2 "0"))))
(defn main []
(println "Enter a three-digit positive integer: ")
(print-digits (Integer/parseInt (read-line)))) AND THAT, MY FRIENDS, IS THE POWER OF RECURSION
Name:
Anonymous2009-02-04 21:14
how come we have no mods on /prog/ ? seriously?
Name:
Anonymous2009-02-04 21:37
faggot :: (Integral a) => a -> [a] -> [a]
faggot a b
| a < 10 = a : b
| otherwise = faggot (quot a 10) ((mod a 10) : b)
import java.util.Scanner;
/**
* An object of class Digit represents one digit in a three digit number
*/
public class Digit {
/**
* Main method which gets a valid number from the standard stream out outputs a series of lines pertaining to the value of each place digit in said number
*
* @param args the command line arguments for this program, they are not used
*/
private static final int NUMDIGITS = 3;
private static final String[] digits = {"ones","tens","hundreds","thousands","ten thousands","hundred thousands","millions"};
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String number = "";
Digit[] digits = new Digit[NUMDIGITS];
while(true) {
System.out.print("Enter a "+NUMDIGITS+" digit positive integer: ");
number = scan.nextLine();
if(getNumber(number)) {
break;
}
else {
System.out.println();
System.out.println("You didn't enter a "+NUMDIGITS+" digit number! try again");
}
}
for(int i=0; i<NUMDIGITS; i++) {
digits[i] = new Digit(i, Integer.parseInt((new Character(number.charAt(i))).toString()));
}
for(Digit digit : digits) {
System.out.println(digit.toString());
}
}
/**
* Static method to parse a string entered by the user and determine if it is a valid number and three digits long
*
* @param number the string representing the input by the user
* @param writeNum the Integer in which this method should write the output integer
* @return true if it is a three digit number, otherwise false
*/
private static boolean getNumber(String number) {
boolean returnval = false;
try {
Integer.parseInt(number);
if(number.length()!=NUMDIGITS) {
throw new NumberFormatException("Number is not "+NUMDIGITS+" digits!");
}
returnval = true;
}
catch(NumberFormatException e) {
}
return returnval;
}
private int position;
private int value;
private String digitValue;
/**
* Constuctor for objects of type Digit
*
* @param position the position of this digit in the three digit string it is a child of
* @param value the single digit value of this digit
*/
public Digit(int position, int value) {
this.position = position;
this.value = value;
this.digitValue = "The "+digits[NUMDIGITS-position-1]+" digit is: ";
}
/**
* Overridden toString() method to convert this digit to a textual representation of what it represents
*
* @return the string representation
*/
public String toString() {
return this.digitValue+new Integer(this.value).toString();
}
}
Name:
Anonymous2009-02-04 23:15
>>13
Actually, it's not that bad. I wouldn't say it's really ENTERPRISE-quality. I've seem much worse by people who were trying.
>>13 private static final int NUMDIGITS = 3;
I'm afraid you'll have to put this in a ConstantFactory that loads constants from an XML database on another server. Also for true, false, and 0.
Name:
Anonymous2009-02-05 14:24
Speaking of true false, languages that implement true as 1 and not -1 are for faggots. The opposite of all zeros is all ones, which is -1.
Name:
Anonymous2009-02-05 14:37
>>18
Screw your signed two's complement integers, negative numbers are for emo faggots. Go cut yourself while I'm here with my happy positive numbers.