我是一名新程序员。我找到了这个随机按钮游戏。我理解这里发生的大部分事情,但
i
做了什么..一旦它i=1
但是它说while(i<=8)
..和a[i]
。 如果有人能解释,我将不胜感激。
namespace Shuffle_Numere
{
public partial class Form1 : Form
{
int num;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (button2.Text == "")
{
button2.Text = button1.Text;
button1.Text = "";
}
if (button4.Text == "")
{
button4.Text = button1.Text;
button1.Text = "";
}
ChekWin();
}
private void button2_Click(object sender, EventArgs e)
{
if (button1.Text == "")
{
button1.Text = button2.Text;
button2.Text = "";
}
if (button3.Text == "")
{
button3.Text = button2.Text;
button2.Text = "";
}
if (button5.Text == "")
{
button5.Text = button2.Text;
button2.Text = "";
}
ChekWin();
}
private void button3_Click(object sender, EventArgs e)
{
if (button2.Text == "")
{
button2.Text = button3.Text;
button3.Text = "";
}
if (button6.Text == "")
{
button6.Text = button3.Text;
button3.Text = "";
}
ChekWin();
}
private void button4_Click(object sender, EventArgs e)
{
if (button1.Text == "")
{
button1.Text = button4.Text;
button4.Text = "";
}
if (button5.Text == "")
{
button5.Text = button4.Text;
button4.Text = "";
}
if (button7.Text == "")
{
button7.Text = button4.Text;
button4.Text = "";
}
ChekWin();
}
private void button5_Click(object sender, EventArgs e)
{
if (button2.Text == "")
{
button2.Text = button5.Text;
button5.Text = "";
}
if (button4.Text == "")
{
button4.Text = button5.Text;
button5.Text = "";
}
if (button6.Text == "")
{
button6.Text = button5.Text;
button5.Text = "";
}
if (button8.Text == "")
{
button8.Text = button5.Text;
button5.Text = "";
}
ChekWin();
}
private void button6_Click(object sender, EventArgs e)
{
if (button3.Text == "")
{
button3.Text = button6.Text;
button6.Text = "";
}
if (button5.Text == "")
{
button5.Text = button6.Text;
button6.Text = "";
}
if (button9.Text == "")
{
button9.Text = button6.Text;
button6.Text = "";
}
ChekWin();
timer1.Enabled = true;
timer1.Start();
}
private void button7_Click(object sender, EventArgs e)
{
if (button4.Text == "")
{
button4.Text = button7.Text;
button7.Text = "";
}
if (button8.Text == "")
{
button8.Text = button7.Text;
button7.Text = "";
}
ChekWin();
}
private void button8_Click(object sender, EventArgs e)
{
if (button5.Text == "")
{
button5.Text = button8.Text;
button8.Text = "";
}
if (button7.Text == "")
{
button7.Text = button8.Text;
button8.Text = "";
}
if (button9.Text == "")
{
button9.Text = button8.Text;
button8.Text = "";
}
ChekWin();
timer1.Enabled = true;
timer1.Start();
}
private void button9_Click(object sender, EventArgs e)
{
if (button6.Text == "")
{
button6.Text = button9.Text;
button9.Text = "";
}
if (button8.Text == "")
{
button8.Text = button9.Text;
button9.Text = "";
}
ChekWin();
}
public void ChekWin()
{
num = num + 1;
label2.Text = num.ToString();
if (button1.Text == "1" && button2.Text == "2" && button3.Text == "3" && button4.Text == "4" && button5.Text == "5" && button6.Text == "6" && button7.Text == "7" && button8.Text == "8" && button9.Text == "")
{
if (MessageBox.Show("Congratulations! You won in "+num+" moves asnd "+timp+" seconds.", "Message text", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
label2.Text = "0";
timp = 0;
timer1.Stop();
label4.Text = "0";
}
}
}
private void button11_Click(object sender, EventArgs e)
{
Application.Exit();
}
public void Shuffle()
{
int i, j, Rn;
int[] a = new int[9];
Boolean flag = false;
i = 1;
do
{
Random rnd = new Random();
Rn = ((rnd.Next(0, 8)) + 1);
for (j = 1; j <= i; j++)
{
if (a[j] == Rn)
{
flag = true;
break;
}
}
if (flag == true)
{
flag = false;
}
else
{
a[i] = Rn;
i = i+1;
}
}
while (i <= 8);
button1.Text =Convert.ToString(a[1]);
button2.Text = Convert.ToString(a[2]);
button3.Text = Convert.ToString(a[3]);
button4.Text = Convert.ToString(a[4]);
button5.Text = Convert.ToString(a[5]);
button6.Text = Convert.ToString(a[6]);
button7.Text = Convert.ToString(a[7]);
button8.Text = Convert.ToString(a[8]);
button9.Text = "";
num = 0;
label2.Text = "0";
timer1.Stop();
label4.Text = "0";
}
private void button10_Click(object sender, EventArgs e)
{
Shuffle();
}
int timp = 0;
private void timer1_Tick(object sender, EventArgs e)
{
timp++;
label4.Text = timp.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
Shuffle();
}
}
}
答案 0 :(得分:1)
这里使用“我”是什么
i
作为计数器和索引变量。
然后它说“while(i&lt; = 8)”..
作为计数器,它用于while-loop或do-while-loop
变量将在循环内递增(值将增加),只要条件(i<=8)
保持true
它在这里扮演什么角色:“a [i]”。
这里它被用作索引变量。这意味着它用于表示数组a
中的位置。这是使用[ ]
operator
正如您所看到的,i
在此行中递增:
i = i+1;
基本上它用于漫游数组并访问它的不同位置。
编辑:抱歉监督旗帜部分我不明白“旗帜”的作用是什么
标志基本上就是信号。它们用于为某些操作设置条件。通常,当满足某个事件发生的另一个条件时,信号被触发/设置为true
。在你的情况下,当位置a
的{{1}}中的数字等于随机抽取的数字时,标志会被提升或信号。
j
这结束了for循环并阻止执行else块:
if (a[j] == Rn)
{
flag = true;
break;
}