这就是问题:
编写程序,以便用户可以输入一笔金额,程序将读回给用户,说明正确的数千,数百和分的数量。
例如:
输入:R123.99
产量:一百二十三兰特和九十九美分。
假设输入金额将始终为rands,如果输入金额为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;
}
}
它有一个逻辑错误,但我不能为我的生活绕过它打印小数,请帮助我
答案 0 :(得分:1)
虽然您在问题中添加了部分代码,但我觉得很难处理这种解决问题的方法。
所以我提出了以下代码来满足您的要求,检查它是否满足您的所有要求。请注意,我只添加了十进制到String转换方法。您可以根据需要在代码中使用它。
请注意,我在班上添加了以下方法。
getString
方法将返回0到19之间的单词表示
getTeen
方法用于获取10,30,40 ... 90的单词表示
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"));
}
}
这些是样本编号的输出。
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
答案 1 :(得分:0)
你错过了这个
intNum = Integer.parseInt(strInput);
在JOptionPane.showInputDialog方法之后粘贴它。
你的开关盒看起来也不正确。如果intNum是1234
然后你会得到
num1 = "Four"
num2 = ""
num3 = ""
num4 = "One Thousand"
num5 = "Four"
num6 = ""
可能不是你想要的。