使用java代码实现将整数转化成英语语法
时间: 2023-05-19 10:03:11 浏览: 87
Java实现数字转成英文的方法
Sure, here's a sample Java code that converts an integer to its English language equivalent:
```
import java.util.Scanner;
public class NumberToWords {
private static final String[] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
private static final String[] tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println(convertNumberToWords(number));
}
public static String convertNumberToWords(int number) {
if (number == 0) {
return "zero";
}
if (number < 0) {
return "minus " + convertNumberToWords(Math.abs(number));
}
String words = "";
if ((number / 1000000) > 0) {
words += convertNumberToWords(number / 1000000) + " million ";
number %= 1000000;
}
if ((number / 1000) > 0) {
words += convertNumberToWords(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0) {
words += convertNumberToWords(number / 100) + " hundred ";
number %= 100;
}
if (number > 0) {
if (number < 20) {
words += ones[number];
} else {
words += tens[number / 10] + " " + ones[number % 10];
}
}
return words.trim();
}
}
```
You can run this code and enter an integer to get its English language equivalent.
阅读全文