编写一个控制台程序,编程从键盘输入三位数字,输出大写形式,

时间:2022-05-20 19:52:15 阅读: 最新文章 文档下载
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。


class Program {

static void Main(string[] args) {

string sInput = ""; string sOutput;

while (sInput != "exit") {

Console.Write("\r\n输入一个三位数(100~999),输入exit结束:");

sInput = Console.ReadLine(); if (sInput != "exit") {

fun f = new fun(sInput);

sOutput = f.myConvertSum(); if (string.IsNullOrEmpty(sOutput)) Console.Write("无法转换,请确认输入的是一个三位数(100~999!");

else

Console.Write("转换结果:{0}", sOutput); }

else {

break; } } } }

class fun {

string snum="";

public fun(string s) {

snum = s; }

public string myConvertSum() { if (!IsPositveDecimal(snum))


return "";

return Convert3Digit(int.Parse(snum)); }

///

/// 判断是否是3位数正数字字符串 ///


/// name= "str "> 判断字符串 /// 如果是数字,返回true否则返回false private bool IsPositveDecimal(string str) {

int d; try {

d = int.Parse(str);

}

catch (Exception) {

return false; }

if (d >= 100 && d <= 999) //限定为3位数 return true; else

return false; }

///

/// 转换三位数字100-999 中文大写 ///


private string Convert3Digit(int num) {

const string chineseNum = "零壹贰叁肆伍陆柒捌玖";

string rstring = "";

if (num < 100 || num > 999) return rstring; int ge = num % 10;

int shi = (num % 100 - ge) / 10; int bai = num / 100; if (ge != 0)


{

rstring = chineseNum.Substring(ge, 1); if (shi != 0)

rstring = chineseNum.Substring(shi, 1) + "" + rstring; else

rstring = chineseNum.Substring(shi, 1) + rstring; }

else if (shi != 0) {



} } }

rstring = chineseNum.Substring(shi, 1) + "" + rstring; }

rstring = chineseNum.Substring(bai, 1) + "" + rstring; return rstring;

本文来源:https://www.wddqw.com/doc/fb6d5625bcd126fff7050bec.html