Java 用文字(包括小数)打印出金额

Java 用文字(包括小数)打印出金额,java,switch-statement,decimal,Java,Switch Statement,Decimal,问题是: 编写一个程序,以便用户可以输入一个金额,该程序将读回给用户,说明数千、数百和美分的正确大小 例如: 输入:R123.99 产量:一百二十三兰特九十九美分 假设输入量始终以兰特为单位,如果输入量为x,则10000>x>=0 这是我目前的代码: import javax.swing.*; import java.util.*; public class switchEnhanced { public static void main(String[] args){

问题是:

编写一个程序,以便用户可以输入一个金额,该程序将读回给用户,说明数千、数百和美分的正确大小

例如:
输入:R123.99
产量:一百二十三兰特九十九美分

假设输入量始终以兰特为单位,如果输入量为x,则10000>x>=0

这是我目前的代码:

import javax.swing.*;
import java.util.*;


public class switchEnhanced
{

    public static void main(String[] args){
        boolean corrInput = true; 
        String strInput = ""; 
        String num1 = ""; //0 to 19
        String num2 = ""; //10s to 90s
        String num3 = ""; //100s to 900s
        String num4 = ""; //1000s to 10 000s
        String num5 = ""; //cents 0-9
        String num6 = ""; //cents 10-90s 
        int intNum = 0;
        int rands = Integer.parseInt(strInput.split("\\.")[0]);
        int cents = Integer.parseInt(strInput.split("\\.")[1]);



        while (corrInput){
            try{

               strInput = JOptionPane.showInputDialog("Type in an amount : ");
                if (intNum>=0 && intNum <=9999){
                    corrInput = false;
                }
             }
            catch(NumberFormatException ex)
            {
                System.out.println(strInput);
                corrInput = true;
            }
        }



        switch(intNum % 10){ 
            case 0: num1 = "Zero";break;
            case 1: num1 = "One";break;
            case 2: num1 = "Two";break;
            case 3: num1 = "Three";break;
            case 4: num1 = "Four";break;
            case 5: num1 = "Five";break;
            case 6: num1 = "Six";break;
            case 7: num1 = "Seven";break;
            case 8: num1 = "Eight";break;
            case 9: num1 = "Nine";break;
            default: break;
        }

        switch(intNum){ 
            case 10: num1 = "Ten";break;
            case 11: num1 = "Eleven";break;
            case 12: num1 = "Twelve";break;
            case 13: num1 = "Thriteen";break;
            case 14: num1 = "Fourteen";break;
            case 15: num1 = "Fifteen";break;
            case 16: num1 = "Sixteen";break;
            case 17: num1 = "Seventeen";break;
            case 18: num1 = "Eighteen";break;
            case 19: num1 = "Nineteen";break;
            default: break;
        }

        switch (intNum/10){
            case 2: num2 = "Twenty";break;
            case 3: num2 = "Thirty";break;
            case 4: num2 = "Fourty";break;
            case 5: num2 = "Fifty";break;
            case 6: num2 = "Sixty";break;
            case 7: num2 = "Seventy";break;
            case 8: num2 = "Eighty";break;
            case 9: num2 = "Ninety";break;
            default: break;
        }

        switch (intNum/100){ 
            case 1: num3 = "One Hundred";break;
            case 2: num3 = "Two Hundred";break;
            case 3: num3 = "Three Hundred";break;
            case 4: num3 = "Four Hundred";break;
            case 5: num3 = "Five Hundred";break;
            case 6: num3 = "Six Hundred";break;
            case 7: num3 = "Seven Hundred";break;
            case 8: num3 = "Eight Hundred";break;
            case 9: num3 = "Nine Hundred";break;
            default: break;

        }

        switch (intNum/1000){ 
            case 1: num4 = "One Thousand";break;
            case 2: num4 = "Two Thousand";break;
            case 3: num4 = "Three Thousand";break;
            case 4: num4 = "Four Thousand";break;
            case 5: num4 = "Five Thousand";break;
            case 6: num4 = "Six Thousand";break;
            case 7: num4 = "Seven Thousand";break;
            case 8: num4 = "Eight Thousand";break;
            case 9: num4 = "Nine Thousand";break;
            default: break;

        }

        switch(intNum % 10){ 
            case 0: num5 = "Zero";break;
            case 1: num5 = "One";break;
            case 2: num5 = "Two";break;
            case 3: num5 = "Three";break;
            case 4: num5 = "Four";break;
            case 5: num5 = "Five";break;
            case 6: num5 = "Six";break;
            case 7: num5 = "Seven";break;
            case 8: num5 = "Eight";break;
            case 9: num5 = "Nine";break;
            default: break;
        }

        switch(intNum){ 
            case 10: num5 = "Ten";break;
            case 11: num5 = "Eleven";break;
            case 12: num5 = "Twelve";break;
            case 13: num5 = "Thriteen";break;
            case 14: num5 = "Fourteen";break;
            case 15: num5 = "Fifteen";break;
            case 16: num5 = "Sixteen";break;
            case 17: num5 = "Seventeen";break;
            case 18: num5 = "Eighteen";break;
            case 19: num5 = "Nineteen";break;
            default: break;

        }

        switch (intNum/10){ 
            case 2: num6 = "Twenty";break;
            case 3: num6 = "Thirty";break;
            case 4: num6 = "Fourty";break;
            case 5: num6 = "Fifty";break;
            case 6: num6 = "Sixty";break;
            case 7: num6 = "Seventy";break;
            case 8: num6 = "Eighty";break;
            case 9: num6 = "Ninety";break;
            default: break;

        }

        if(num2.length()== 0){
            System.out.println(num1 + num6);
        }
        else if(num6.length()== 0){
            System.out.println(num1 + num6);
        }
        else
        {
            System.out.println(num4 +" "+ num3 +" "+ num2 +" "+ num1 + "Rand" +" "+ num6 +" "+ num5 + "Cents");
        }
        return;
    }
}
import javax.swing.*;
导入java.util.*;
公共类交换机增强
{
公共静态void main(字符串[]args){
布尔corrInput=true;
字符串strInput=“”;
字符串num1=“”;//0到19
字符串num2=“”;//10s到90s
字符串num3=“”;//100s到900s
字符串num4=“”;//1000秒到10000秒
字符串num5=”“;//0-9美分
字符串num6=“”;//分10-90秒
intNum=0;
int rands=Integer.parseInt(strInput.split(“\\”)[0]);
int cents=Integer.parseInt(strInput.split(“\\”)[1]);
while(corrInput){
试一试{
strInput=JOptionPane.showInputDialog(“键入金额:”);
如果(intNum>=0&&intNum您缺少此

intNum = Integer.parseInt(strInput);
将其粘贴到JOptionPane.showInputDialog方法之后

你的开关盒看起来也不正确。如果intNum是1234

然后你会得到

num1 = "Four"
num2 = ""
num3 = ""
num4 = "One Thousand"
num5 = "Four"
num6 = ""

可能不是您想要的。

虽然您在问题中添加了部分代码,但我觉得很难处理这种方法来解决您的问题

因此,我提出了以下代码来满足您的要求,检查它是否满足您的所有要求。请注意,我只添加了十进制到字符串的转换方法。您可以根据自己的意愿在代码中使用它

注意,我在类中添加了以下方法

getString
方法将返回从0到19的单词表示形式

用于获取10,30,40…90的单词表示的
getTeen
方法

用于获取整数的单词表示的
getWholeWord
方法

用于获取十进制数字的单词表示形式的
getDecimalValue
方法

您可以浏览下面的代码。我已经在代码上添加了内联注释

public class MyDecimalToString {

public static String getString(int number){
    switch(number){
    case 0:
        return "Zero";
    case 1:
        return"One";
    case 2:
        return"Two";
    case 3:
        return"Three";
    case 4:
        return"Four";
    case 5:
        return"Five";
    case 6:
        return"Six";
    case 7:
        return"Seven";
    case 8:
        return"Eight";
    case 9:
        return"Nine";
    case 10:
        return "Ten";
    case 11:
        return "Eleven";
    case 12:
        return "Twelve";
    case 13:
        return "Thriteen";
    case 14:
        return "Fourteen";
    case 15:
        return "Fifteen";
    case 16:
        return "Sixteen";
    case 17:
        return "Seventeen";
    case 18:
        return "Eighteen";
    case 19:
        return "Nineteen";      
    }
    return "";
}

public static String getTeen(int num){

    switch (num) {
    case 2:
        return "Twenty";
    case 3:
        return "Thirty";
    case 4:
        return "Fourty";
    case 5:
        return "Fifty";
    case 6:
        return "Sixty";
    case 7:
        return "Seventy";
    case 8:
        return "Eighty";
    case 9:
        return "Ninety";
    }
    return "";

}

//This method will provide whole number string representation 
public static String getWholeWord( int number){

    String output="";
    int input=0;

    String inputNumberString=String.valueOf(number);

    int lastNum=Integer.valueOf(inputNumberString.substring(inputNumberString.length()-1,inputNumberString.length()));
    int numberBeforeLast=(number>9?(Integer.valueOf(inputNumberString.substring(inputNumberString.length()-2,inputNumberString.length()-1))):0);

    if(number>=1000){
        input=number/1000;
        output=getWholeWord(input)+" Thousand ";            
    }
    input=number%1000;
    if(input>=100){
        int tempNum=input/100;
        output+=getString(tempNum)+" Hundread ";
    }

    if(numberBeforeLast>0){
        int tempNum=input;
        if(numberBeforeLast==1){
            tempNum=Integer.valueOf(String.valueOf(numberBeforeLast)+lastNum);
            output+=getString(tempNum);
            lastNum=0;
            numberBeforeLast=1;
        }else{
            output+=getTeen(numberBeforeLast)+" ";                          
        }
    }

    if(lastNum>0){
        output+=getString(lastNum)+" ";
    }else if(numberBeforeLast==0&&number<100){
        output+=getString(lastNum)+" ";
    }
    return output;
}

//This method will return decimal value String representation 
public static String getDecimalValue(String decimal){
    String output="";

    //check whether the decimal string contains fractions
    if(decimal.contains(".")){

        //Identify the fraction and non fraction parts in decimal
        String partBeforeDecimalPoint=decimal.split("\\.")[0];
        String partAfterDecimalPoint=decimal.split("\\.")[1];

        if(partBeforeDecimalPoint.length()>0)
            output=getWholeWord(Integer.parseInt(partBeforeDecimalPoint));

        if(partAfterDecimalPoint.length()>0)
            output+= ((output.length()>0?" and ":"")+getWholeWord(Integer.parseInt(partAfterDecimalPoint))+"cents");

    }else{
        output=getWholeWord(Integer.parseInt(decimal));
    }
    return output; 
}

public static void main(String[] args) {

    System.out.println(getDecimalValue("12.20"));
    System.out.println(getDecimalValue("2100.99"));
    System.out.println(getDecimalValue("4500.67"));
    System.out.println(getDecimalValue("23450"));
    System.out.println(getDecimalValue("15"));

}
}

除了在IDE调试器中一次一行地跨过代码一行,你永远不会把<代码> ItnNU<代码>分配给你。这是开始的地方。考虑设置断点和调试你的代码。你会发现答案更快。way@JimGarrison是的。您需要转换strInput并将intNum设置为该值。非常感谢H
Twelve and Twenty cents
Two  Thousand One Hundread  and Ninety Nine cents
Four  Thousand Five Hundread  and Sixty Seven cents
Twenty Three  Thousand Four Hundread Fifty 
Fifteen