while循环、console.clear、break、continue的用法

本周的学习进度很快,下面主要说几点:

1.while(true)的用法,while(true)是一个死循,他常常需要结合Console.Clear()结合使用,用来实现控制台的清屏和重复提示。

例如:我们要求在控制台显示获取用户输入年月的提示,之后在控制台输出对应的日历;这时,如果用户再需要看下一月的日历,那么我们就需要一个控制台清屏后再输出的一个效果。怎么实现呢?

while(true)

{

  //提示用户输入并获取输入

  //输出对应的日历

  //等待用户重新输入

  //清屏动作也就是:

  Console.Clear();  

}

这样就能保证日历的一个重新获取

2.break和continue的用法,先说下区别,break结束整个循环而continue是值结束当前的循环,例如:

for(int i=0;i<8;i++)

{

  a=a+i;

  //break或continue

}

在此处如果我们使用的是break,那么for的整个循环就结束了,计算机开始执行下一条代码;而如果我们使用的是continue,那么这里就是指结束当前i的运算,开始下一步也就是i+1的运算。

3.我们常把while(true)和break/continue跟clear一起使用,用来实现:提示用户输入错误,请用户重新数如

转载于:https://www.cnblogs.com/qlongbg/p/9162388.html

好的,我可以给你提供一个简单的c#五子棋小游戏的示例代码,规则如下: 1. 两个玩家分别使用黑子和白子,轮流落子; 2. 落子时,玩家需要输入坐标,如“3,2”表示在第3行第2列落子; 3. 当一方连成五子时,游戏结束,显示胜利信息; 4. 如果棋盘填满仍未分出胜负,则宣布平局。 下面是示例代码: ```csharp using System; namespace Gomoku { class Program { static int[,] board = new int[15, 15]; // 棋盘,0表示空,1表示黑子,2表示白子 static int currentPlayer = 1; // 当前玩家,1表示黑子,2表示白子 static void Main(string[] args) { Console.WriteLine("欢迎来到五子棋游戏!"); DrawBoard(); while (true) { Console.WriteLine($"请 {GetPlayerName(currentPlayer)} 落子(输入坐标,如“3,2”):"); string input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { Console.WriteLine("无效输入!"); continue; } string[] parts = input.Split(','); if (parts.Length != 2 || !int.TryParse(parts[0], out int row) || !int.TryParse(parts[1], out int col)) { Console.WriteLine("无效输入!"); continue; } if (row < 1 || row > 15 || col < 1 || col > 15) { Console.WriteLine("坐标超出范围!"); continue; } if (board[row - 1, col - 1] != 0) { Console.WriteLine("该位置已有棋子,请重新输入!"); continue; } board[row - 1, col - 1] = currentPlayer; DrawBoard(); if (CheckWin(row - 1, col - 1)) { Console.WriteLine($"恭喜 {GetPlayerName(currentPlayer)} 获胜!"); break; } if (CheckDraw()) { Console.WriteLine("平局!"); break; } currentPlayer = currentPlayer == 1 ? 2 : 1; } Console.WriteLine("按任意键结束游戏..."); Console.ReadKey(); } static void DrawBoard() { Console.Clear(); Console.WriteLine(" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"); for (int i = 0; i < 15; i++) { Console.Write($"{i + 1} "); for (int j = 0; j < 15; j++) { switch (board[i, j]) { case 0: Console.Write("+ "); break; case 1: Console.Write("● "); break; case 2: Console.Write("○ "); break; } } Console.WriteLine(); } } static bool CheckWin(int row, int col) { int count = 1; int x, y; // 横向 x = col - 1; y = row; while (x >= 0 && board[y, x] == currentPlayer) { count++; x--; } x = col + 1; while (x < 15 && board[y, x] == currentPlayer) { count++; x++; } if (count >= 5) { return true; } // 纵向 count = 1; x = col; y = row - 1; while (y >= 0 && board[y, x] == currentPlayer) { count++; y--; } y = row + 1; while (y < 15 && board[y, x] == currentPlayer) { count++; y++; } if (count >= 5) { return true; } // 左上到右下 count = 1; x = col - 1; y = row - 1; while (x >= 0 && y >= 0 && board[y, x] == currentPlayer) { count++; x--; y--; } x = col + 1; y = row + 1; while (x < 15 && y < 15 && board[y, x] == currentPlayer) { count++; x++; y++; } if (count >= 5) { return true; } // 右上到左下 count = 1; x = col + 1; y = row - 1; while (x < 15 && y >= 0 && board[y, x] == currentPlayer) { count++; x++; y--; } x = col - 1; y = row + 1; while (x >= 0 && y < 15 && board[y, x] == currentPlayer) { count++; x--; y++; } if (count >= 5) { return true; } return false; } static bool CheckDraw() { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (board[i, j] == 0) { return false; } } } return true; } static string GetPlayerName(int player) { return player == 1 ? "黑子" : "白子"; } } } ``` 希望这个示例代码可以帮助你了解如何使用c#编写一个简单的五子棋小游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值