從鍵盤輸入一串整數,按數字的相反順序輸出
{
static void Main(string[] args)
{
Sort s1 = new Sort();
while (true)
{
Console.WriteLine("請輸入一組小於10位的數字,將會逆序列印");
s1.InputNum();
if (!s1.Judge()) break;
Console.WriteLine();
}
Console.ReadKey();
}
}
class Sort
{
//輸入的方法
public void InputNum(){
//儲存輸入的字串
string s = Console.ReadLine();
//儲存轉換後的數字
int i = 0;
//判斷是不是數字串
bool isNum = false;
if (int.TryParse(s, out i))//將字串轉換成整數 返回布林型 轉換成功返回true 轉換失敗返回false
{
int nextNum = i;
int thisNum = 0;
Console.Write("輸出結果是:");
for (int j = 0; j < s.Length; j++)
{
thisNum = nextNum % 10;
nextNum = nextNum / 10;
Console.Write(thisNum);
}
Console.WriteLine();
}
else {
Console.WriteLine("輸入的不是一串數字");
}
}
//判斷是否繼續輸入
public bool Judge()
{
Console.WriteLine("是否繼續輸入:(Y/N)");
string c = Console.ReadLine();
c = c.ToUpper();
if (c == "Y") {
return true;
}
else if (c == "N")
{
return false;
}
else {
Console.WriteLine("輸入錯誤");
return false;
}
}
}