我是靠谱客的博主 柔弱冰淇淋,这篇文章主要介绍C#while循环,现在分享给大家,希望可以做个参考。

while语句按不同条件执行一个嵌入语句零次或多次。
格式:while(布尔类型表达式)嵌入式语句循环体
实例:输入两个数字,若和为100则继续输入,若不为100则退出游戏,最后的分数为输入100的次数。
代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace whilesample_10._13_ { class Program { static void Main(string[] args) { int score = 0; bool canContinue = true; while(canContinue) { Console.WriteLine("Please input first number:"); string str1 = Console.ReadLine(); int x = int.Parse(str1); Console.WriteLine("Please input second number:"); string str2 = Console.ReadLine(); int y = int.Parse(str2); int sum = x + y; if(sum==100) { score++; Console.WriteLine("Correct!{0}+{1}={2}",x,y,sum); } else { Console.WriteLine("Error!{0}+{1}={2}",x,y,sum); canContinue = false; } } Console.WriteLine("Your score is {0}",score); Console.WriteLine("Game Over!"); } } }

程序结果为:
在这里插入图片描述

最后

以上就是柔弱冰淇淋最近收集整理的关于C#while循环的全部内容,更多相关C#while循环内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部