我被分配编写一种方法,该方法的货币价值小于$ 1000.00,并写入等价的单词。此金额也需要包括小数点后的部分(本质上是金额的变化部分)。即
Enter·check·amount:567.89↵
five·hundred·sixty·seven·and·89/100↵
我看到该主题已经讨论了很多次,但是我似乎找不到我所要求的任何信息。我是这新手(4周),因此,如果我提出错误的问题或以错误的方式陈述自己的所作所为,请原谅我。我可以以字为单位输出金额(从结果中可以看到),但我认为我遇到的问题在于用户输入Double且我的代码中不包含任何要求<1000.00的代码。我的主要方法确实包含双精度数,但是每次我尝试将moneyWord方法中的Int类型更改为Double时,如果If语句中的Int指出不能将Int转换为Double(我知道)。另外,我认为我的退货声明可能需要包含更多信息。再一次,我可能会朝错误的方向寻找错误,但是我认为这是我要去的地方。此外,我认为某些材料可能会在以后的课程中讲授,因为我不得不研究课本中尚未涉及的章节-数组。这是我的代码。任何帮助,将不胜感激
//program to write the word equivalent of a check amount
import java.util.Scanner;
public class CheckToWord {
public static void main(String[] args) { // main method
double number = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the check amount:"); // prompt user to enter check amount
number = scanner.nextDouble();
if (number == 0) {
System.out.print("Zero");
} else {
System.out.print("" + moneyWord((int) number)); // output amount in words
}
}
private static String moneyWord(int number) {
String words = ""; // variable to hold string representation of number
String onesArray[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
String tensArray[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty",
"ninety" };
if (number < 0) { // convert the number to a string
String numberStr = "" + number;
numberStr = numberStr.substring(1); // remove minus before the number
return "minus " + moneyWord((int) Double.parseDouble(numberStr)); // add minus before the number and convert
// the rest of number
}
if ((number / 1000) > 0) { // check if number is divisible by 1 thousand
words += moneyWord(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0) { // check if number is divisible by a hundred
words += moneyWord(number / 100) + " hundred ";
number %= 100;
}
if (number < 20) { // check if number is within the teens
words += onesArray[number]; // get the appropriate value from ones array
} else {
words += tensArray[number / 10]; // get the appropriate value from tens array
if ((number % 10) > 0) {
words += "-" + onesArray[number % 10];
}
}
return words;
}
}
结果
Please enter the check amount:
1523.23
one thousand five hundred twenty-three
尝试一下:
public class CheckToWord {
public static void main(String[] args) { //main method
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the check amount:"); //prompt user to enter check amount
double number = scanner.nextDouble();
String[] parsed = parse(Double.toString(number));
int main = Integer.parseInt(parsed[0]);
int change = Integer.parseInt(parsed[1]);
if (main == 0 && change == 0) {
System.out.print("Zero");
} else {
String mwm = moneyWord(main);
String mwc = moneyWord(change);
System.out.println("" + mwm + " and " + mwc + " cents");
}
}
private static String[] parse(String number) {
String[] split = number.contains(".") ? number.split(Pattern.quote(".")) : new String[]{number, "00"};
String main = split[0];
String change = split[1].length() >= 2 ? split[1].substring(0, 2) :
split[1].length() == 1 ? split[1] + "0" : "00";
return new String[]{main, change};
}
private static String moneyWord(int number) {
if(number > 1000) {
throw new IllegalArgumentException("Number value should be less than 1000");
}
String words = ""; // variable to hold string representation of number
String onesArray[] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen"};
String tensArray[] = {"zero", "ten", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
if (number < 0) { // convert the number to a string
String numberStr = "" + number;
numberStr = numberStr.substring(1); // remove minus before the number
return "minus " + moneyWord((int) Double.parseDouble(numberStr)); // add minus before the number and convert the rest of number
}
if ((number / 1000) > 0) { // check if number is divisible by 1 thousand
words += moneyWord(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0) { // check if number is divisible by a hundred
words += moneyWord(number / 100) + " hundred ";
number %= 100;
}
if (number < 20) { // check if number is within the teens
words += onesArray[number]; // get the appropriate value from ones array
} else {
words += tensArray[number / 10]; // get the appropriate value from tens array
if ((number % 10) > 0) {
words += "-" + onesArray[number % 10];
}
}
return words;
}
}
示例输出:
Please enter the check amount:
10.25
ten and twenty-five cents
将变更与全部金额分开的另一种可能性如下。
double[] vals = { 22.45, 1222.0, 122, .97, 2900.98
};
for (double val : vals) {
int whole = (int) val;
// get remainder, multiply by 100 and add .5 to round up
// prior to casting.
int change = (int) ((val % 1) * 100 + .5);
// if change is 0, use xx in place
System.out.printf("%7.2f : %5d and %s/100%n",
val,
whole,
(change == 0) ? "xx"
: change + "");
}