C#编程:输入一个4位数的正整数,编写程序,输出这个数的千位,百位,十位,个位。

求个位时要用到模数运算符。。。。。

你是指在DOS界面运行的那种程序吗?
还是web上的。
Web上的话,可以直接获取输入框中的数字转换成int类型,再求余获取。

int Mat = Int.Parse(TextBox1.Text.Trim());
int xxxx = Mat / 1000; //例如 4321 / 1000 = 4
int xxx = Mat % 1000 / 100; 例如 4321 % 1000 = 321 321 / 100 = 3
int xx = Mat % 100 / 10; 例如 4321 % 100 = 21 21 / 10 = 2
int x = Mat % 10; 例如 4321 % 10 = 1

DOS那种页面的话,要先获取你输入的数字。
也就是要先Consolute.Read()一下。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-07-27
千位:x.substring(0,1);
百位:x.substring(1,1);
十位:x.substring(2,1);
个位:x.substring(3,1);
第2个回答  2011-07-27
int a=TextBox1.Text,q=0,b=0,s=0,g=0;
q=a\1000;
b=(a\100)%10;
s=(a\10)%10;
g=a%10;