在班级里找赵六,并送他回家。
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
第一种:
//for循环找人
//初始化数组
string[] classStudents = new string[] { "张三", "李四", "王五", "赵六", "田七", "周八" };
//获取数组长度
int studentscount = classStudents.Length;
//for循环便利数组
for (int i = 0; i < studentscount; i++)
{
//遍历到元素赵六,进入循环
if (classStudents[i] == "赵六")
{
MessageBox.Show("找到了" + classStudents[i] + "送他回家");
break;//跳出循环
}
}
第二种:
//while循环找人
//初始化数组
string[] classstudents = new string[] { "张三", "李四", "王五", "赵六", "田七", "周八" };
int i = 0;
string student = "";
bool feifei = true;
while (student != "赵六")
{
if (i >= classstudents.Length)
{
MessageBox.Show("没有要找的人");
feifei = false;
break;
}
student = classstudents[i++];
}
if (feifei = true)
MessageBox.Show("找到了" + student + "送他回家");
}
}
}