2016-12-08 10 views
-2

これは質問です:ユーザーがプログラムがバック何千もの正しい大きさを述べるユーザーに読まれます金額を入力することができるように(小数点を含む)の言葉で金額を印刷

はプログラムを書きます、何百、そしてセント。例えば


入力:R123.99
出力:百と23ランズ九十9セント。

入力量が常に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; 
    } 
} 

それは論理的なエラーを持っていますが、私は私の人生のためにカント小数をプリントアウトし、それを回避する、あなたはこれを欠けている

+1

0以外のものには決して割り当てません –

+1

IDEデバッガで一度に1行ずつコードをステップ実行しましたか?それが始まる場所です。 –

+1

ブレークポイントの設定とコードのデバッグを検討してください。あなたは答えが早くそのように見つけるだろう –

答えて

1

あなたの質問にコードの一部を追加しましたが、あなたの問題を解決するためのそのようなアプローチを扱うのは難しいと感じました。

私はあなたの要件を満たすために次のコードを思いついたので、すべての要件を満たしているかどうかを確認します。私は文字列変換メソッドに10進数を追加しただけであることに注意してください。あなたはあなたのコードでこれを使うことができます。

私のクラスでは以下のメソッドを追加しています。

getString方法は、0から19

に10,30,40のための単語表現を取得するために使用getTeen方法を単語表現を返します... 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 
+0

ありがとうございました! –

0

に私を助けてください

intNum = Integer.parseInt(strInput); 

JOptionPane.showInputDialogメソッドの後に貼り付けます。

スイッチのケースも正しく表示されません。 INTNUM 1234

であるならば、あなたが望んでいないものを、おそらく

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

を取得します。

関連する問題