1、依次提示用户输入两个整数(假设i1,i2)。如果i1,i2都是正数,则将i1的值递增一个数,然后打印i1+i2的值;如果i1,i2都是负数,则将i1的值递减10个数,然后打印i1*i2的值;如果i1,i2中任一个为0,则提示数据有错误,否则计算i1*i2的绝对值。代码如下:
namespace test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个整数:");
string stri1 = Console.ReadLine();
int i1 = Convert.ToInt32(stri1);
Console.WriteLine("请再次输入一个整数:");
string stri2 = Console.ReadLine();
int i2 = Convert.ToInt32(stri2);
if (i1 > 0 && i2 > 0)
{
i1 = i1+1;
Console.WriteLine("{0}+{1}={2}",i1,i2,i1+i2);
}
else if (i1 < 0 && i2 < 0)
{
i1 = i1 - 10;
Console.WriteLine("{0}*{1}={2}",i1,i2,i1*i2);
}
else if (i1 == 0 || i2 == 0)
{
Console.WriteLine("数据有误,请从新输入:");
}
else
{
int i3=i1*i2;
if (i3 < 0)
{
i3 = -i3;
}
Console.WriteLine(i3);
}
Console.ReadKey();
}
}
}
2、要求用户输入用户名密码,只要不是admin和888888,就一直提示要求重新输入。代码如下:
Console.WriteLine("请输入用户名:");
string user =Console.ReadLine();
Console.WriteLine("请输入密码:");
string pwd = Console.ReadLine();
while (user != "admin" || pwd != "888888") //注意字符串的写法,"admin"
{
Console.WriteLine("您输入的用户名或密码错误,请重新输入:");
Console.WriteLine("请重新输入用户名:");
user = Console.ReadLine();
Console.WriteLine("请重新输入密码:");
pwd = Console.ReadLine();
}
Console.WriteLine("欢迎登录!");
Console.ReadKey();
3、不断要求用户输入一个数字,然后打印这个数字的二倍,当用户输入q的时候程序退出(return)。代码如下:
while (true)
{
Console.WriteLine("请输入一个数字");
string strinput = Console.ReadLine();
if (strinput == "q") //if语句的位置很重要,如果把if语句放在后面就会出现BUG,原因是"q"不能被转换为int型
{
return; //执行到return的时候就退出整个函数(Main),while也就不会继再继续执行下去了,而控制台程序一旦Main退出程序也就退出了
}
int input = Convert.ToInt32(strinput);
Console.WriteLine("所输入的数字的两倍是:{0}", input * 2);
}
Console.ReadKey();
4、不断要求用户输入一个数字(假定用户输入的都是正整数),当用户输入end的时候显示刚才输入的数字中的最大值。设一个变量int max,初始值为0,用户每输入一次就把用户输入的和max比较一下,如果输入的必max大,则让max等于用户输入。代码如下:
int max = 0;
while(true)
{
Console.WriteLine("请输入一个正整数(结束输入end):");
string input = Console.ReadLine();
if (input == "end")
{
Console.WriteLine("所输入的最大值为{0}", max); //注意if语句的位置和Console.ReadKey();+return;的位置
Console.ReadKey();
return;
}
int num = Convert.ToInt32(input);
if (num > max)
{
max = num;
}
}
5、string[] strs={"aa","435","sdf","tre"}返回给我一个字符串,然后字符串使用我指定的分隔符来进行分割,比如我指定用“|”分割,那么返回给我
“aa|435|sdf|tre”。
static void Main(string[] args)
{
string[] strs ={"aa","435","sdf","tre"};
Console.WriteLine(Test(strs,"|"));
Console.ReadKey();
}
static string Test(string[] strs,string seperator)
{
string abc = "" ;
for(int i=0;i
{
abc =abc + strs[i] + seperator ;
}
abc = abc + strs[strs.(length-1)] ;
return abc ;
}
6、从一个整数数组中取出最大的整数
static void Main(string[] args)
{
//定义一个整数数组
int[] arr = { 3, 6, 7, 2, 1, 0,8 };
int max = arr[0];
for (int i = 1; i < arr.Length; i++)
{
//求最大数
if (arr[i] > max)
{
max = arr[i];
}
}
Console.WriteLine("最大的整数是:"+max);
Console.ReadLine();
}
7、将int数组中的奇数放到一个新的int数组中返回
static void Main(string[] args)
{
int[] numbers = {2,7,9,4,3,8,1,6 };
List listOld = new List();
for (int i = 0; i < numbers.Length; i++)
{
//提取数组中的奇数
if (numbers[i] % 2 != 0)
{
listOld.Add(numbers[i]);
}
}
//将listOld中的数组复制到新的数组中
int[] allNum = listOld.ToArray();
for (int i = 0; i < allNum.Length; i++)
{
Console.WriteLine(allNum[i]);
}
Console.ReadLine();
}
8、 //计算字符串中每种字符出现的次数(面试题)。 //Welcome to Chinaworld;不区分大小写
static void Main(string[] args)
{
string world = "Welcome to Chinaworld";
//将字符串转为小写形式
world = world.ToLower();
//建立泛型集合
Dictionary dict = new Dictionary();
//将字符串变成字符数组
char[] chs = world.ToCharArray();
//遍历每一个字符
for (int i = 0; i < chs.Length; i++)
{
//通过char的方法判断是否为字母,如果为字母,则进行下面的操作
if (char.IsLetter(chs[i]))
{
//如果不包含该键,则加入集合中
if (!dict.ContainsKey(chs[i]))
{
//将每个字符加入集合中对应键,其值初始为1
dict.Add(chs[i], 1);
}
else
{
//否则,包含该键,只将其对应的值+1即可
dict[chs[i]] = dict[chs[i]] + 1;
}
}
}
//循环遍历输出集合中的键 值
foreach (KeyValuePair kv in dict)
{
Console.WriteLine("字母:{0}出现了{1}次。",kv.Key,kv.Value);
}
Console.ReadLine();
}