2015-10-17 30 views
-2

無論輸入是什麼,它都應該用文字打印答案(無限制)例如輸入是2,它應該用文字「TWO」回答幾個例子瞭解12 =「十二」,51 =「五一」,1000 =「一千」,2005 =「二千五」等等......創建以用戶輸入數字作爲輸入並將其打印在文字上的ac#程序

+0

@dustmouse沒有沒有...我真的很困惑,甚至如何開始... –

+0

舊任務的學生嗎? – alerya

+0

遵循此:http://stackoverflow.com/questions/3213/convert-integers-to-written-numbers 或http://www.codeproject.com/Articles/15934/How-to-Convert-a-Numeric -Value-or-Currency-to-Engl or http://www.blackbeltcoder.com/Articles/strings/converting-numbers-to-words – autopilot

回答

0

退房很好Humanizer項目。它正是你所需要的。

3501.ToWords() => "three thousand five hundred and one" 
0
Try this approach: 
It's not a complete code, you can improve it by recursive call with 
proper range checking etc. 

Create a function with input parameter range from 0 to 99 (as shown below): 
private string DigitToTextConverter(int digit) 
    { 
     string digitText = ""; 
     switch (digit) 
     { 
      case 0: 
       digitText = "zero"; 
       break; 
      case 1: 
       digitText = "one "; 
       break; 
      case 2: 
       digitText = "two "; 
       break; 
      case 3: 
       digitText = "three "; 
       break; 
      case 4: 
       digitText = "four "; 
       break; 
      case 5: 
       digitText = "five "; 
       break; 
      case 6: 
       digitText = "six "; 
       break; 
      case 7: 
       digitText = "seven "; 
       break; 
      case 8: 
       digitText = "eight "; 
       break; 
      case 9: 
       .....; 
      case 10: 
       ..... 
      case 99: 
       ..... 
      default: 
       break; 
     } 

     return digitText; 
    } 

//Call this function with appropriate parameter: (suppose user entered 1234) 

var userInput = 1234; 
var digitText = new StringBuilder(); 

var quotient = userInput/1000; 
string convertedText = DigitToTextConverter(quotient); 
digitText.Append(convertedText + " thousand"); 
digitText.AppendFormat(" "); 
var remainder = userInput % 1000; 

quotient = remainder/100; 
convertedText = DigitToTextConverter(quotient); 
digitText.Append(convertedText + " thundred"); 
digitText.AppendFormat(" "); 
remainder = remainder % 100; 

//Complete remaining portion, better to have a function with recursive call 
string finalText = digitText.ToString(); 
相關問題