1.猜拳游戏
/*做个猜拳游戏
* 要求1:要有相关的提示信息,比如,用户准备出拳
* 要球2:主程序页面要有退出按钮
* 可选:如果用户积分少于100,则提示用户信息积分不足直接退出游戏,如果超出100可以进行猜拳游戏,用户每赢一次,加分50,输一次减50
*
* 规则:0:剪刀
* 1 包子
* 2 锤子
*
* 电脑出拳随机0-2
*
* 判断结果:0 0 提示打平
* 0 1用户赢:积分+50
* 0 2电脑赢 积分-50
*/
代码实现
Console.WriteLine ("0:剪刀 1 包子 2 锤子");
Console.WriteLine ("按8退出游戏");
int jf = 200;
bool s = true;
while (s) {
int a = int.Parse (Console.ReadLine ());
switch (a) {
case 0:
Random ran = new Random ();
int RandKey = ran.Next (0, 3);
if (RandKey == 0) {
Console.WriteLine ("打平");
Console.WriteLine ("电脑出拳为:剪刀");
Console.WriteLine ("用户出拳为:剪刀");
}
if (RandKey == 1) {
Console.WriteLine ("用户赢");
Console.WriteLine ("电脑出拳为:包子");
Console.WriteLine ("用户出拳为:剪刀");
jf += 50;
}
if (RandKey == 2) {
Console.WriteLine ("电脑赢");
Console.WriteLine ("电脑出拳为:锤子");
Console.WriteLine ("用户出拳为:剪刀");
jf -= 50;
}
break;
case 1:
Random ra = new Random ();
int Rand = ra.Next (0, 3);
if (Rand == 0) {
Console.WriteLine ("用户赢");
Console.WriteLine ("电脑出拳为:剪刀");
Console.WriteLine ("用户出拳为:包子");
}
if (Rand == 1) {
Console.WriteLine ("打平");
Console.WriteLine ("电脑出拳为:包子");
Console.WriteLine ("用户出拳为:包子");
jf -= 50;
}
if (Rand == 2) {
Console.WriteLine ("用户赢");
Console.WriteLine ("电脑出拳为:锤子");
Console.WriteLine ("用户出拳为:包子");
jf += 50;
}
break;
case 2:
Random r = new Random ();
int Ra = r.Next (0, 3);
if (Ra == 0) {
Console.WriteLine ("用户赢");
Console.WriteLine ("电脑出拳为:剪刀");
Console.WriteLine ("用户出拳为:锤子");
jf += 50;
}
if (Ra == 1) {
Console.WriteLine ("电脑赢");
Console.WriteLine ("电脑出拳为:包子");
Console.WriteLine ("用户出拳为:锤子");
jf -= 50;
}
if (Ra == 2) {
Console.WriteLine ("打平");
Console.WriteLine ("电脑出拳为:锤子");
Console.WriteLine ("用户出拳为:锤子");
}
break;
case 8:
s = false;
break;
default:
break;
}
Console.WriteLine ("积分为:{0}",jf);
}
2. (**)找出下列整型数组中最大和最小值及其所在位置i。
int[] a={3,5,8,13,47,9};
int[] b ={-23,45,14,65};
int max = a [0];
int min = a [0];
int index = 0;
int index1 = 0;
for (int i = 0; i
max = a [i];
index = i;
Console.WriteLine (max);
} else if (min > a [i]) {
min = a [i];
index1 = i;
}
}
Console.WriteLine ("最大值:{0} 所在位置:{1}最小值:{2} 所在位置:{3}",max,index,min,index1);
}
3. (**)已知abc+cba = 1333,其中a,b,c均为一位数,编程求出满足条件的a,b,c所有组合。
for (int a = 0; a <= 9; a++) {
for (int b = 0; b <=9; b++) {
for (int c = 0; c <=9; c++) {
if(((a*100)+(b*10+c))+((c*100)+(b*10)+(a))==1333)
Console.WriteLine ("{0} {1} {2}",a,b,c);
}
}
}
4. (****)输入n,分别用*输出边长为n的实心菱形和空心菱形。
例如:n = 3时,输出:
*
***
*****
***
*
int n = 5;
for (int i = 0; i < 2 * n - 1; i++) {
if (i < n) {
for (int j = 0; j < n + i; j++) {
if (j < n - i - 1) {
Console.Write (" ");
} else {
Console.Write ("*");
}
}
Console.WriteLine ();
}
if (i >= n) {
for (int j = 0; j < n + (2 * n - i) - 2; j++) {
if (j < n - (2 * n - i - 1)) {
Console.Write (" ");
} else {
Console.Write ("*");
}
}
Console.WriteLine ();
}
}
*
* *
* *
* *
*
int n = 11;
for (int i = 0; i < 2 * n - 1; i++) {
if (i < n) {
for (int j = 0; j < n + i; j++) {
if (j < n - i - 1) {
Console.Write (" ");
} else if (j == (n - i - 1) || j == (n + i -1)) {
Console.Write ("*");
} else {
Console.Write (" ");
}
}
Console.WriteLine ();
}
if (i >= n) {
for (int j = 0; j < n + (2 * n - i) - 2; j++) {
if (j < n - (2 * n - i - 1)) {
Console.Write (" ");
} else if (j == n - (2 * n - i - 1) || j == n + (2 * n - i) - 3) {
Console.Write ("*");
} else {
Console.Write (" ");
}
}
Console.WriteLine ();
}
}
5. 分别用while、do...while、for循环打印1~100之间3的倍数
//for
for (int i = 1; i < 101; i++)
{
if (i % 3 == 0)
{
Console.WriteLine (i);
}
}
// do....while
int i=1;
do {
if(i%3==0)
{
Console.WriteLine (i);
}
i++;
} while(i < 101);
int a = 1;
while (a<101) {
if (a % 3 == 0) {
Console.WriteLine (a);
}
a++;
}
6. 用while和for循环打印出1-100之间7的倍数,个位为7的数,十位为7的倍数,不是7的倍数并且不包含7
for (int a = 1; a <= 100; a++) {
if (a % 7 == 0) {
Console.WriteLine ("7的倍数:{0}",a);
}
if (a%10==7) {
Console.WriteLine ("个位7的数:{0}",a);
}
if (a%100/10==7) {
Console.WriteLine ("十位7的数:{0}",a);
}
if (a % 7 != 0 && a % 10 != 7 && a % 100 / 10 != 7) {
Console.WriteLine ("不是7的倍数并且不包含7:{0}",a);
}
}
7. 使用嵌套循环产生下列图案:
$
$$
$$$
$$$$
$$$$$
for (int i = 1; i <=5; i++) {
for (int j = 1; j <=i; j++) {
Console.Write ('$');
}
Console.WriteLine ();
}
8. 使用循环打印杨辉三角
*
***
*****
int n = 5;
for (int i = 1; i <= n; i+=2)
{
Console.Write(new string(' ', n - i));
for (int j = 1; j <= 2 * i; j++)
Console.Write(" *"[j % 2]);
Console.WriteLine();
}
}