gpt4 book ai didi

java - 如何将支票转换为包含小数位的文字

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:08 25 4
gpt4 key购买 nike

我被分配了一个任务,要求编写一个方法,该方法接受小于 1000.00 美元的货币值,并写出与该金额相当的单词。该金额还需要包括小数点后的部分(本质上是金额的找零部分)。

Enter·check·amount:567.89↵
five·hundred·sixty·seven·and·89/100↵

我发现该主题已被多次讨论,但我似乎找不到有关我所请求内容的任何信息。我是新手(4 周),所以如果我问了错误的问题或以错误的方式陈述了我所做的事情,请原谅我。我能够以文字输出金额(正如您从我的结果中看到的那样),但我相信我遇到的问题在于用户输入 Double 并且我的代码不包含任何请求 <1000.00 的内容。我的主要方法确实包含 double 字,但每次我尝试将 MoneyWord 方法中的 Int 类型更改为 Double 时,我的 If 语句中的 Int 数字表明 int 无法转换为 Double(我知道)。另外,我认为我的返回声明可能需要包含更多信息。再说一次,我可能正在寻找错误的方向,但我相信这就是我出错的地方。此外,我认为其中一些 Material 可能会在未来的类(class)中教授,因为我必须研究教科书中尚未涵盖的章节 - 即数组。这是我的代码。任何帮助将不胜感激

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

关于java - 如何将支票转换为包含小数位的文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59120820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com