循环语句主要有:for;while;foreach。最常用的是for循环。for循环的四要素:初始条件;循环条件;循环体;状态改变。for循环的顺序是:初始条件--循环条件--循环体--状态改变。break是跳出循环。用几个习题演示一下for循环。

1、让用户输入一个100以内的数
打印1-100之间所有的数,用户输入的数除外

namespace _2017_2_23作业1打印100以内的数
{
class Program
{
static void Main(string[] args)
{int i;int b=;
Console.WriteLine("请输入一个100以内的数");
b=Convert.ToInt32(Console.ReadLine());
for ( i = ; i < ;i++ )
{
if( i!=b)
{
Console.WriteLine(i);
} }
Console.ReadLine();
}
}
}

2、让用户输入一个100以内的数
打印1-这个数之间所有的数的和

namespace _2017_2_23打印1到输入数之间的和
{
class Program
{
static void Main(string[] args)
{
int i; int b = ; int count = ;
Console.WriteLine("请输入一个100以内的数:");
b=Convert.ToInt32(Console.ReadLine());
for (i = ; i < ;i++ )
{
count = count + i;
if(i==b) break; }
Console.WriteLine(count); Console.ReadLine();
} }
}

3、打印100以内所有的质数/素数,再求和


namespace _2017_2_23_作业3__100以内的质数素数求和
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
for (int j = 2; j <= 100;j++ )
{
int count = 0;
for (int i = 1; i <= j; i++)
{
if (j % i == 0)
{

count++;
}


}
if (count == 2)
{
sum += j;
Console.WriteLine(j);
}

}
Console.WriteLine(sum);
Console.ReadLine();
}

}
}

 

4、使用一个for循环,分别打印出来100以内的奇数和偶数,分别求和
奇数:1,3,5,7.....
偶数:2,4,6,8.....
奇数和:xxx
偶数和:xxx

namespace _2017_2_23分别打印100以内的奇数_偶数并求和
{
class Program
{
static void Main(string[] args)
{
String jishu = "";
string oushu = "";
int jishu1 = ;
int oushu1 = ; for (int a = ; a <= ;a++ )
{
if (a % == )
{
jishu += a+","; jishu1 += a;
} else
{
oushu += a+","; oushu1 += a;
}
}
Console.WriteLine("奇数:" +jishu);
Console.WriteLine("偶数:" + oushu);
Console.WriteLine("奇数和:"+jishu1);
Console.WriteLine("偶数和:" + oushu1); Console.ReadLine();
}
}
}

5、猜拳(三局两胜)
请输入您的手势:石头
用户手势:石头 电脑手势:剪刀
用户胜:1 电脑胜:0

请输入您的手势:石头
用户手势:石头 电脑手势:石头
用户胜:1 电脑胜:0

请输入您的手势:石头
用户手势:石头 电脑手势:包袱
用户胜:1 电脑胜:1

请输入您的手势:石头
用户手势:石头 电脑手势:剪刀
用户胜:2 电脑胜:1
用户胜利!!!

namespace _2017_2_23猜拳_三局两胜
{
class Program
{ static void Main(string[] args)
{
int u = ; int c = ;
for (; ; )
{
Console.Write("请输入您的手势(石头/剪子/包袱):");
String you = Console.ReadLine();
Random n = new Random();
int a = n.Next(, ); if (a == )
{
Console.WriteLine("石头");
}
if (a == )
{
Console.WriteLine("剪子");
}
if (a == )
{
Console.WriteLine("包袱");
} if ((you == "石头" && a == ) || (you == "剪子" && a == ) || (you == "包袱" && a == ))
{
Console.WriteLine("你赢了");
u++;
}
else if ((you == "石头" && a == ) || (you == "剪子" && a == ) || (you == "包袱" && a == ))
{
Console.WriteLine("平了");
}
else
{
Console.WriteLine("你输了");
c++;
}
if(u==)
{
Console.WriteLine("用户最后胜利");
}
else if(c==)
{
Console.WriteLine("电脑最后胜利");
}
}
Console.ReadLine(); }
}
}

最新文章

  1. java 判断两个list是否相等
  2. jQuery EasyUI教程之datagrid应用(一)
  3. com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
  4. C语言extern作用(全局变量)
  5. Effective Java 56 Adhere to generally accepted naming conventions
  6. Python操作Mysql实例代码教程在线版(查询手册)
  7. mac 下 配置 xhprof
  8. JavaScript trim 实现(去除字符串首尾指定字符)
  9. [LeetCode#12] Roman to Integer
  10. HDOJ/HDU 1865 1sting(斐波拉契+大数~)
  11. Java接口和抽象类的实现方法
  12. 为用户增加sudo权限(修改sudoers文件)
  13. Android-Volley网络通信框架(ImageRequest,ImageLoader,NetWorkImageView)
  14. Java经典编程题50道之十七
  15. 一步一步设置Joomla!开发环境
  16. rsyslog及loganalyzer
  17. Flask开发基础
  18. ABP框架系列之十六:(Dapper-Integration-Dapper集成)
  19. 奇葩问题 eclipse中DDMS的LOGcat只有一列level
  20. GCC编译器ABI

热门文章

  1. ios页面跳转
  2. DDD之:Repository仓储模式
  3. 安卓selector
  4. C#、C++用GDAL读shp文件(转载)
  5. 让表格table呈现边框,不用给tr、td加边框的写法
  6. 2.11. 创建托管对象(Core Data 应用程序实践指南)
  7. C++ STL算法系列1---unique , unique_copy函数
  8. swift webView的高度自适应内容
  9. CRS-2800 CRS-4000
  10. jdk自带的动态代理