Java 如何在我的程序中以文字形式输出一百一十几个数字
我需要帮助以文字形式显示我的百年不遇数字。例如,如果我输入116。我的程序将输出106,而不是116。除青少年数字外,我输入的所有其他数字都有效。以防万一,你不想这样做,:Java 如何在我的程序中以文字形式输出一百一十几个数字,java,methods,do-while,Java,Methods,Do While,我需要帮助以文字形式显示我的百年不遇数字。例如,如果我输入116。我的程序将输出106,而不是116。除青少年数字外,我输入的所有其他数字都有效。以防万一,你不想这样做,: public static String intToText(int n) { if (n < 0) return "Minus " + intToText(-n); else if (n == 0) return "Zero"; else if (n <=
public static String intToText(int n) {
if (n < 0)
return "Minus " + intToText(-n);
else if (n == 0)
return "Zero";
else if (n <= 19)
return oneToNineteen[n - 1] + " ";
else if (n <= 99)
return twentyToNinety[n / 10 - 2] + " " + intToText(n % 10);
else if (n <= 199)
return "One Hundred " + intToText(n % 100);
else if (n <= 999)
return intToText(n / 100) + "Hundred " + intToText(n % 100);
else if (n <= 1999)
return "One Thousand " + intToText(n % 1000);
else if (n <= 999999)
return intToText(n / 1000) + "Thousand " + intToText(n % 1000);
else if (n <= 1999999)
return "One Million " + intToText(n % 1000000);
else if (n <= 999999999)
return intToText(n / 1000000) + "Million " + intToText(n % 1000000);
else if (n <= 1999999999)
return "One Billion " + intToText(n % 1000000000);
else
return intToText(n / 1000000000) + "Billion " + intToText(n % 1000000000);
}
公共静态字符串intToText(intn){
if(n<0)
返回“减”+intToText(-n);
else如果(n==0)
返回“零”;
否则,如果(n发生这种情况是因为在检查数字是否在[100119]范围内之前先检查数字是否在[100999]范围内,请更改ifs的顺序,它将正常工作。在比较时从最小数字开始到最大数字
第一个条件:
if((numInput>=10)&&(numInput<=19)){
如果((numInput>=10)&&&(numInput=20)&&&(numInput=100)&&(numInput=100)&&&(numInput您应该将
else if((numInput>100)&&(numInput<=119))
else if((numInput>100)&&&(numInput=100)&&(numInput您的else if
else if((numInput>100)&&(numInput<=119)){
hundreds(hundredsDigit);
System.out.print(" ");
teens(numInput);
}
否则如果((numInput>100)&&&(numInput=100)&&(numInput我将更改代码中的4项:
第一名:
使用int
而不是double
进行输入
int numInput = Integer.parseInt(br.readLine());//user inputs number
秒:
要在int
中获得适当的数字位置,请使用:
int hundredsDigit = (numInput % 1000) / 100;
int tensDigit = (numInput % 100) / 10;
int onesDigit = numInput % 10;
而不是:
double hundredsDigit=Math.floor((numInput%1000)/100);
double tensDigit = Math.floor((numInput % 100) / 10);
double onesDigit = numInput % 10;
第三名:
110-119范围的else
条件必须在100-999之前(技术上应为120-999)
第四名:
您的方法将原始numInput作为参数
您需要使用的是onesDigit
,以确定它是哪个“青少年”
所以这应该是一个类似于:
teens(onesDigit);
必须在[10-19]条件和[110-119]条件下更改此调用
您的新方法应该如下所示:
public static void teens(int onesDigit) {
if (onesDigit == 0) {
System.out.print("Ten ");
}
if (onesDigit == 1) {
System.out.print("Eleven ");
}
if (onesDigit == 2) {
System.out.print("Twelve ");
}
if (onesDigit == 3) {
System.out.print("Thirteen ");
}
if (onesDigit == 4) {
System.out.print("Fourteen ");
}
if (onesDigit == 5) {
System.out.print("Fifteen ");
}
if (onesDigit == 6) {
System.out.print("Sixteen ");
}
if (onesDigit == 7) {
System.out.print("Seventeen ");
}
if (onesDigit == 8) {
System.out.print("Eighteen ");
}
if (onesDigit == 9) {
System.out.print("Nineteen ");
}
}//closes teens method
这很有趣。它有以下问题:
-仅处理整数。无长、双精度等。
-不处理零的单一情况。
-在Integer.MIN_值处失败,因为数字=数字*-1
否则,它似乎会起作用。其动机是我无法忍受大量的“if-else”代码
public class NumbersToWords{
私有静态最终映射NUM_TO_WORD=new HashMap();
私有静态最终列表键=新的ArrayList(30);
静止的{
NUM_TO_WORD.put(0,“零”);
数字到单词put(1,“一”);
数字到单词put(2,“2”);
数到字,放(3,“三”);
数到字,放(4,“四”);
数到字,放(5,“5”);
数字到单词的put(6,“六”);
数字到单词put(7,“七”);
数到字的put(8,“8”);
数字到单词put(9,“九”);
数字到单词的put(10,“十”);
数字到单词put(11,“11”);
数字到单词put(12,“12”);
数字到单词put(13,“十三”);
数字到单词put(14,“十四”);
数字到单词put(15,“十五”);
数字到单词put(16,“十六”);
数字到单词put(17,“17”);
数字到单词put(18,“18”);
数字到单词put(19,“十九”);
数字到单词put(20,“二十”);
数字到单词put(30,“三十”);
数字到单词put(40,“四十”);
数字到单词put(50,“五十”);
数字到单词put(60,“六十”);
数字到单词put(70,“70”);
数字到单词put(80,“80”);
数字到单词put(90,“90”);
数字到单词put(100,“百”);
字数(1000,“千”);
字数(1000000,百万);
字对字的认沽权(1000000000,“十亿”);
addAll(NUM_TO_WORD.keySet());
集合。排序(键);
}
公共静态void main(字符串[]args){
int[]testValues={24,4543755,121000012300123,Integer.MAX_VALUE,-456};
NumbersToWords ntw=新的NumbersToWords();
for(int i:testValues){
System.out.println(i+“->”+ntw.getWords(i));
}
}
/*递归调用*/
公共字符串getWords(整数){
布尔值为负=数值<0;
如果(为负){
数字=数字*-1;
}
如果(数量<100){
返回GetWordlessThan一百(数字);
}
StringBuilder单词=新StringBuilder(50);
int key=getKey(数字);
如果(为负){
词语。附加(“否定”);
}
words.append(getWords(数字/键))
.append(“”).append(NUM_TO_WORD.get(key))//获取最大的占位符单词
.append(“”).append(getWords(number%key));//获取其余内容
返回words.toString();
}
私有字符串GetWordlessThan一百(整数){
如果(数字==0){
返回“”;
}
如果(数字<21){
将NUM_返回到WORD.get(number);
}
int key=getKey(数字);
返回NUM_TO_WORD.get(key)+“”+NUM_TO_WORD.get(number-key);
}
私有整数getKey(整数编号){
对于(int i=0;i 0&&number
我将非常感谢您对我的帮助。请格式化您的代码以使用适当的缩进。您的代码片段非常难理解。
int numInput = Integer.parseInt(br.readLine());//user inputs number
int hundredsDigit = (numInput % 1000) / 100;
int tensDigit = (numInput % 100) / 10;
int onesDigit = numInput % 10;
double hundredsDigit=Math.floor((numInput%1000)/100);
double tensDigit = Math.floor((numInput % 100) / 10);
double onesDigit = numInput % 10;
teens(onesDigit);
public static void teens(int onesDigit) {
if (onesDigit == 0) {
System.out.print("Ten ");
}
if (onesDigit == 1) {
System.out.print("Eleven ");
}
if (onesDigit == 2) {
System.out.print("Twelve ");
}
if (onesDigit == 3) {
System.out.print("Thirteen ");
}
if (onesDigit == 4) {
System.out.print("Fourteen ");
}
if (onesDigit == 5) {
System.out.print("Fifteen ");
}
if (onesDigit == 6) {
System.out.print("Sixteen ");
}
if (onesDigit == 7) {
System.out.print("Seventeen ");
}
if (onesDigit == 8) {
System.out.print("Eighteen ");
}
if (onesDigit == 9) {
System.out.print("Nineteen ");
}
}//closes teens method
public class NumbersToWords {
private static final Map<Integer,String> NUM_TO_WORD = new HashMap<Integer, String>();
private static final List<Integer> KEYS = new ArrayList<Integer>(30);
static {
NUM_TO_WORD.put(0,"zero");
NUM_TO_WORD.put(1,"one");
NUM_TO_WORD.put(2,"two");
NUM_TO_WORD.put(3,"three");
NUM_TO_WORD.put(4,"four");
NUM_TO_WORD.put(5,"five");
NUM_TO_WORD.put(6,"six");
NUM_TO_WORD.put(7,"seven");
NUM_TO_WORD.put(8,"eight");
NUM_TO_WORD.put(9,"nine");
NUM_TO_WORD.put(10,"ten");
NUM_TO_WORD.put(11,"eleven");
NUM_TO_WORD.put(12,"twelve");
NUM_TO_WORD.put(13,"thirteen");
NUM_TO_WORD.put(14,"fourteen");
NUM_TO_WORD.put(15,"fifteen");
NUM_TO_WORD.put(16,"sixteen");
NUM_TO_WORD.put(17,"seventeen");
NUM_TO_WORD.put(18,"eighteen");
NUM_TO_WORD.put(19,"nineteen");
NUM_TO_WORD.put(20,"twenty");
NUM_TO_WORD.put(30,"thirty");
NUM_TO_WORD.put(40,"forty");
NUM_TO_WORD.put(50,"fifty");
NUM_TO_WORD.put(60,"sixty");
NUM_TO_WORD.put(70,"seventy");
NUM_TO_WORD.put(80,"eighty");
NUM_TO_WORD.put(90,"ninety");
NUM_TO_WORD.put(100,"hundred");
NUM_TO_WORD.put(1000,"thousand");
NUM_TO_WORD.put(1000000,"million");
NUM_TO_WORD.put(1000000000,"billion");
KEYS.addAll(NUM_TO_WORD.keySet());
Collections.sort(KEYS);
}
public static void main(String[] args){
int[] testValues = {24,4,543755,12,10000,123000,123,Integer.MAX_VALUE, -456};
NumbersToWords ntw = new NumbersToWords();
for(int i : testValues){
System.out.println(i + " -> " + ntw.getWords(i));
}
}
/* called recursively */
public String getWords(int number){
boolean isNegative = number < 0;
if(isNegative){
number = number * -1;
}
if(number < 100){
return getWordLessThanHundred(number);
}
StringBuilder words = new StringBuilder(50);
int key = getKey(number);
if(isNegative){
words.append("negative ");
}
words.append(getWords(number/key))
.append(" ").append(NUM_TO_WORD.get(key)) // get the largest placeholder word
.append(" ").append(getWords(number % key)); // get the rest
return words.toString();
}
private String getWordLessThanHundred(int number){
if(number == 0){
return "";
}
if(number < 21){
return NUM_TO_WORD.get(number);
}
int key = getKey(number);
return NUM_TO_WORD.get(key) + " " + NUM_TO_WORD.get(number - key);
}
private int getKey(int number){
for(int i = 0; i<KEYS.size();i++){
int value = KEYS.get(i);
if(i > 0 && number < value){
return KEYS.get(i - 1);
}else if(number == value){
return value;
}
}
return KEYS.get(KEYS.size() - 1);
}
}