2017-2-25 练习题总结
1.输出100以内的质数
int sum = 0; for (int i = 2; i <= 100;i++ ) { int count = 0;//下面的循环完成,count重新赋值0;重新赋值之后如果=2;就是质数,后面输出 //判断质数 for (int j = 1; j <= i;j++ ) { if(i%j==0) { count++; } } if(count==2)//count=2,就是质数 { Console.WriteLine(i); sum += i; } } Console.WriteLine(sum); Console.ReadLine();
2.使用一个for循环,让用户输入一个数,输出1到这个数之间的偶数,素数
输出的格式:奇数:1,3,5,……最后一个奇数不带都逗号
偶数:2,4,6,……最后一个偶数不带都逗号
奇数和是:xxx
偶数和是:xxx
string jishu = ""; string oushu = ""; string jishu1 = ""; string oushu1 = ""; Console.Write("请输入一个数字,打印0到这个数之间的奇偶数:"); int user = Convert.ToInt32(Console.ReadLine()); for (int i = 1; i <= user; i++) { if (i % 2 == 1 && i != user && i != user - 1) { jishu += i+","; } else if (i % 2 == 0 && i != user && i != user - 1) { oushu += i+","; } else if (i % 2 == 1 && (i == user || i == user - 1)) { jishu += i; } else if (i % 2 == 0 && (i == user || i == user - 1)) { oushu += i; } } Console.Write("奇数是:"+jishu); Console.WriteLine(); Console.Write("偶数是:"+oushu); Console.ReadLine();
4.打印五角星
“请输入一个奇数:”
不是奇数,提示输入有误
是奇数
★
★★★
★★★★★
★★★★★★★
★★★★★★★★★
★★★★★★★
★★★★★
★★★
★
思路:分别打印所有图形,用for进行拼接
Console.Write("请输入一个奇数:"); int user = Convert.ToInt32(Console.ReadLine()); if (user % 2 == 1) { Console.WriteLine("您输入正确,给您打印图案!!"); for (int i = 1; i <= user; i++) { for (int j = 1; j <= user - i; j++) { Console.Write(" "); } for (int a = 1; a < i; a++) { Console.Write("★"); } for (int j = 1; j <= i; j++) { Console.Write("★"); } Console.WriteLine(); } for (int i = 1; i <= user; i++) { for (int a = 0; a <= i - 1; a++) { Console.Write(" "); } for (int j = 1; j <= user - i; j++) { Console.Write("★"); } for (int j = 1; j <= (user-1) - i; j++) { Console.Write("★"); } Console.WriteLine(); } } else { Console.WriteLine("您输入的的有误,请重新输入"); } Console.ReadLine();
5.猜拳游戏:要求3局2胜,显示用户,电脑出拳的方式,胜利得1分,平局不计分,先达到2分胜出
int countRen = 0; int countCom = 0; Console.WriteLine("-----------------------------------------------------------"); Console.WriteLine("----------------猜拳游戏------------------------------------"); for (; ; ) { Console.Write("请用户输入(0-剪刀 1-石头 2-包袱):"); string user1 = Console.ReadLine(); int user=0; if(user1=="剪刀") { user = 0; } else if (user1 == "石头") { user = 1; } else if (user1 == "包袱") { user = 2; } Random dd = new Random(); int com = dd.Next(0, 3); if (com == 0) { Console.WriteLine("电脑出剪刀"); } else if (com == 1) { Console.WriteLine("电脑出石头"); } else if (com == 2) { Console.WriteLine("电脑出包袱"); } //判断 //0 1 2 0 1 2 //2 0 1 用户赢 1 2 0 if (user - com == -2 || user - com == 1) { countRen++; Console.WriteLine("用户胜利得"+countRen+"分\t电脑得"+countCom); } else if (user - com == -1 || user - com == 2) { countCom++; Console.WriteLine("用户胜利得" + countRen + "分\t电脑得" + countCom); } else if (user == com) { Console.WriteLine("平局"); } if (countRen == 2 || countCom==2) { break; } } Console.WriteLine("请按键判断结果"); Console.ReadLine(); if (countRen > countCom) { Console.Write("用户取得胜利"); } else { Console.Write("电脑取得胜利"); } Console.ReadLine();