我正在使用Windows表单,我有两个列表:一个用于播放器名称,另一个用于玩家猜测的数字。使用此代码,我获得所有玩家的列表框名称,但问题是我只能从最后一个玩家获得与系统生成的数字相同的数字以及玩家猜到的数字,但我需要知道如何获得每个玩家的两个列表中的数字相同,例如:
玩家1:
2,3,28 //列表中有三个相同的数字
玩家2:
1
等。
我写了这段代码:
namespace LojaFatitForm
{
public partial class Loja_e_Fatit : Form
{
public Loja_e_Fatit()
{
InitializeComponent();
}
int player = 1;
int Guessed=0;
int ticket = 1;
string t = "-------------------------------------------------------------------------------------";
List<string> nameOfPlayers = new List<string>();
List<string> choosedNumbers = new List<string>();
List<string> winningNumbers = new List<string>();
List<string> guessedNumbers = new List<string>();
private void Loja_e_Fatit_Load(object sender, EventArgs e)
{ }
private void bntGenerateNr_Click(object sender, EventArgs e)
{
int Mosha;
Int32.TryParse(txtMosha.Text, out Mosha);
if (Mosha < 18)
{
MessageBox.Show("Lojtari duhet te jete ne moshen madhore!");
txtMosha.Text = "";
}
if (string.IsNullOrWhiteSpace(txtMosha.Text) || string.IsNullOrWhiteSpace(txtEmri.Text) || string.IsNullOrWhiteSpace(txtMbiemri.Text) || string.IsNullOrWhiteSpace(txtQyteti.Text)
|| string.IsNullOrWhiteSpace(txtNr1.Text) || string.IsNullOrWhiteSpace(txtNr2.Text) || string.IsNullOrWhiteSpace(txtNr3.Text) || string.IsNullOrWhiteSpace(txtNr4.Text)
|| string.IsNullOrWhiteSpace(txtNr5.Text)|| string.IsNullOrWhiteSpace(txtNr6.Text) || string.IsNullOrWhiteSpace(txtNr7.Text))
{
MessageBox.Show("Ju lutemi jepni te dhenat/Numrat tuaja!");
}
else
if(txtNr2==txtNr1 || txtNr2==txtNr3 )
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr2.Text = "";
}
else
if (txtNr3 == txtNr2 || txtNr3==txtNr1)
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr3.Text = "";
}else
if (txtNr4 == txtNr1 || txtNr4==txtNr3 || txtNr4==txtNr2)
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr4.Text = "";
}else
if (txtNr5 == txtNr1 || txtNr5==txtNr2 || txtNr5==txtNr3 || txtNr5==txtNr4)
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr5.Text = "";
}
else
if (txtNr6 == txtNr1 || txtNr6==txtNr2 || txtNr6==txtNr3 || txtNr6==txtNr4 || txtNr6==txtNr5)
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr6.Text = "";
}else
if (txtNr7 == txtNr1 || txtNr7==txtNr2 || txtNr7==txtNr3 || txtNr7==txtNr4 || txtNr7==txtNr5 || txtNr7==txtNr6)
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr7.Text = "";
}
else
{
Random r = new Random();
int Nr1, Nr2, Nr3, Nr4, Nr5, Nr6, Nr7;
Int32.TryParse(txtNr1Gen.Text, out Nr1);
Nr1 = r.Next(1, 39);
txtNr1Gen.Text = Nr1.ToString();
if ((txtNr1Gen.Text == txtNr1.Text) || (txtNr1Gen.Text == txtNr2.Text) || (txtNr1Gen.Text == txtNr3.Text) ||
(txtNr1Gen.Text == txtNr4.Text) || (txtNr1Gen.Text == txtNr5.Text) || (txtNr1Gen.Text == txtNr6.Text) || (txtNr1Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr1Gen.Text);
Guessed++;
}
Int32.TryParse(txtNr2Gen.Text, out Nr2);
nr2: Nr2 = r.Next(1, 39);
if (Nr2 == Nr1)
{
goto nr2;
}
else
{
txtNr2Gen.Text = Nr2.ToString();
if ((txtNr2Gen.Text == txtNr1.Text) || (txtNr2Gen.Text == txtNr2.Text) || (txtNr2Gen.Text == txtNr3.Text) ||
(txtNr2Gen.Text == txtNr4.Text) || (txtNr2Gen.Text == txtNr5.Text) || (txtNr2Gen.Text == txtNr6.Text) || (txtNr2Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr2Gen.Text);
Guessed++;
}
}
Int32.TryParse(txtNr3Gen.Text, out Nr3);
nr3: Nr3 = r.Next(1, 39);
if (Nr3 == Nr2 || Nr3 == Nr1)
{
goto nr3;
}
else
{
txtNr3Gen.Text = Nr3.ToString();
if ((txtNr3Gen.Text == txtNr1.Text) || (txtNr3Gen.Text == txtNr2.Text) || (txtNr3Gen.Text == txtNr3.Text) ||
(txtNr3Gen.Text == txtNr4.Text) || (txtNr3Gen.Text == txtNr5.Text) || (txtNr3Gen.Text == txtNr6.Text) || (txtNr3Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr3Gen.Text);
Guessed++;
}
}
Int32.TryParse(txtNr4Gen.Text, out Nr4);
nr4: Nr4 = r.Next(1, 39);
if (Nr4 == Nr3 || Nr4 == Nr2 || Nr4 == Nr1)
{
goto nr4;
}
else
{
txtNr4Gen.Text = Nr4.ToString();
if ((txtNr4Gen.Text == txtNr1.Text) || (txtNr4Gen.Text == txtNr2.Text) || (txtNr4Gen.Text == txtNr3.Text) ||
(txtNr4Gen.Text == txtNr4.Text) || (txtNr4Gen.Text == txtNr5.Text) || (txtNr4Gen.Text == txtNr6.Text) || (txtNr4Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr4Gen.Text);
Guessed++;
}
}
Int32.TryParse(txtNr5Gen.Text, out Nr5);
nr5: Nr5 = r.Next(1, 39);
if (Nr5 == Nr4 || Nr5 == Nr3 || Nr5 == Nr2 || Nr5 == Nr1)
{
goto nr5;
}
else
{
txtNr5Gen.Text = Nr5.ToString();
if ((txtNr5Gen.Text == txtNr1.Text) || (txtNr5Gen.Text == txtNr2.Text) || (txtNr5Gen.Text == txtNr3.Text) ||
(txtNr5Gen.Text == txtNr4.Text) || (txtNr5Gen.Text == txtNr5.Text) || (txtNr5Gen.Text == txtNr6.Text) || (txtNr5Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr5Gen.Text);
Guessed++;
}
}
Int32.TryParse(txtNr6Gen.Text, out Nr6);
nr6: Nr6 = r.Next(1, 39);
if (Nr6 == Nr5 || Nr6 == Nr4 || Nr6 == Nr3 || Nr6 == Nr2 || Nr6 == Nr1)
{
goto nr6;
}
else
{
txtNr6Gen.Text = Nr6.ToString();
if ((txtNr6Gen.Text == txtNr1.Text) || (txtNr6Gen.Text == txtNr2.Text) || (txtNr6Gen.Text == txtNr3.Text) ||
(txtNr6Gen.Text == txtNr4.Text) || (txtNr6Gen.Text == txtNr5.Text) || (txtNr6Gen.Text == txtNr6.Text) || (txtNr6Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr6Gen.Text);
Guessed++;
}
}
Int32.TryParse(txtNr7Gen.Text, out Nr7);
nr7: Nr7 = r.Next(1, 39);
if (Nr7 == Nr6 || Nr7 == Nr5 || Nr7 == Nr4 || Nr7 == Nr3 || Nr7 == Nr2 || Nr7 == Nr1)
{
goto nr7;
}
else
{
txtNr7Gen.Text = Nr7.ToString();
if ((txtNr7Gen.Text == txtNr1.Text) || (txtNr7Gen.Text == txtNr2.Text) || (txtNr7Gen.Text == txtNr3.Text) ||
(txtNr7Gen.Text == txtNr4.Text) || (txtNr7Gen.Text == txtNr5.Text) || (txtNr7Gen.Text == txtNr6.Text) || (txtNr7Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr7Gen.Text);
Guessed++;
}
}
updateListbox("");
updateListbox("Random numbers generated are:" + txtNr1Gen.Text + " " + txtNr2Gen.Text + " " + txtNr3Gen.Text + " " + txtNr4Gen.Text + " " + txtNr5Gen.Text + " " + txtNr6Gen.Text + " " + txtNr7Gen.Text);
updateListbox(t);
}
bntGenerateNr.Enabled = false;
btnNewPlayer.Enabled = false;
btnSave.Enabled = false;
foreach (var i in choosedNumbers)
{
foreach (var j in winningNumbers)
{
if (i == j)
{
guessedNumbers.Add(i);
}
}
}
foreach (var i in nameOfPlayers)
{
if (Guessed < 1)
{
updateListbox("There are no winners in this round!");
}
else
{
updateListbox("Player: " + i + " has guessed " + Guessed + " Numbers: ");
updateListbox(string.Join(", ", guessedNumbers));
foreach (var j in guessedNumbers)
{
updateListbox(j);
}
}
}
}
private void updateListbox(string value)
{
Lista.Items.Add(value);
Lista.TabIndex = Lista.Items.Count - 1;
}
private void btnBack_Click(object sender, EventArgs e)
{
MainForm main = new MainForm();
this.Hide();
main.ShowDialog();
this.Close();
}
private void btnNewPlayer_Click(object sender, EventArgs e)
{
txtNr1.Text = null;
txtNr2.Text = null;
txtNr3.Text = null;
txtNr4.Text = null;
txtNr5.Text = null;
txtNr6.Text = null;
txtNr7.Text = null;
txtEmri.Text = "";
txtMbiemri.Text = "";
txtMosha.Text = "";
txtQyteti.Text = "";
txtId.Text = "";
player++;
ticket++;
}
private void btnSave_Click(object sender, EventArgs e)
{
updateListbox("Ticket: " + ticket);
// Validimet per numrat e dhene
if (string.IsNullOrWhiteSpace(txtNr1.Text) || string.IsNullOrWhiteSpace(txtNr2.Text) || string.IsNullOrWhiteSpace(txtNr3.Text) || string.IsNullOrWhiteSpace(txtNr4.Text)
|| string.IsNullOrWhiteSpace(txtNr5.Text) || string.IsNullOrWhiteSpace(txtNr6.Text) || string.IsNullOrWhiteSpace(txtNr7.Text))
{
MessageBox.Show("Give some number at any of 7 numbers");
}
else
if (txtNr1.Text == txtNr2.Text || txtNr1.Text == txtNr3.Text || txtNr1.Text == txtNr4.Text || txtNr1.Text == txtNr5.Text || txtNr1.Text == txtNr6.Text || txtNr1.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 1st!");
txtNr1.Text = "";
}
else
if (txtNr2.Text == txtNr3.Text || txtNr2.Text == txtNr4.Text || txtNr2.Text == txtNr5.Text || txtNr2.Text == txtNr6.Text || txtNr2.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 2nd!");
txtNr2.Text = "";
}
else
if (txtNr3.Text == txtNr4.Text || txtNr3.Text == txtNr5.Text || txtNr3.Text == txtNr6.Text || txtNr3.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 3d!");
txtNr3.Text = "";
}
else
if (txtNr4.Text == txtNr5.Text || txtNr4.Text == txtNr6.Text || txtNr4.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 4th!");
txtNr4.Text = "";
}
else
if (txtNr5.Text == txtNr6.Text || txtNr5.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 5th!");
txtNr5.Text = "";
}
else
if (txtNr6.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 6th!");
txtNr6.Text = "";
}
else
if (Convert.ToInt32(txtNr1.Text) < 1 || Convert.ToInt32(txtNr1.Text) > 39)
{
txtNr1.Text = "";
MessageBox.Show("Please write 1st numbers because is out of range!");
}
else
if (Convert.ToInt32(txtNr2.Text) < 1 || Convert.ToInt32(txtNr2.Text) > 39)
{
txtNr2.Text = "";
MessageBox.Show("Please write 2nd numbers because is out of range!!");
}
else
if (Convert.ToInt32(txtNr3.Text) < 1 || Convert.ToInt32(txtNr3.Text) > 39)
{
txtNr3.Text = "";
MessageBox.Show("Please write 3d numbers because is out of range!!");
}
else
if (Convert.ToInt32(txtNr4.Text) < 1 || Convert.ToInt32(txtNr4.Text) > 39)
{
txtNr4.Text = "";
MessageBox.Show("Please write 4th numbers because is out of range!!");
}
else
if (Convert.ToInt32(txtNr5.Text) < 1 || Convert.ToInt32(txtNr5.Text) > 39)
{
txtNr5.Text = "";
MessageBox.Show("Please write 5th numbers because is out of range!!");
}
else
if (Convert.ToInt32(txtNr6.Text) < 1 || Convert.ToInt32(txtNr6.Text) > 39)
{
txtNr6.Text = "";
MessageBox.Show("Please write 6th numbers because is out of range!!");
}
else
if ((Convert.ToInt32(txtNr7.Text) < 1 || Convert.ToInt32(txtNr7.Text) > 39))
{
txtNr7.Text = "";
MessageBox.Show("Please write 7th numbers because is out of range!!");
}
else
{
updateListbox("Player " + txtEmri.Text + " choosed :" + txtNr1.Text + " " + txtNr2.Text + " " + txtNr3.Text +
" " + txtNr4.Text + " " + txtNr5.Text + " " + txtNr6.Text + " " + txtNr7.Text);
choosedNumbers.Add(txtNr1.Text);
choosedNumbers.Add(txtNr2.Text);
choosedNumbers.Add(txtNr3.Text);
choosedNumbers.Add(txtNr4.Text);
choosedNumbers.Add(txtNr5.Text);
choosedNumbers.Add(txtNr6.Text);
choosedNumbers.Add(txtNr7.Text);
}
nameOfPlayers.Add(txtEmri.Text);
}
}
}
答案 0 :(得分:0)
我假设,因为我们不知道你的updateListbox()
方法的代码是什么,它取代了显示框中的所有内容。
因此,你需要这样的东西:
foreach (var i in emrateLojtarve)
{
if (teQelluar < 1)
{
updateListbox("Ne kete gjiro nuk ka fituar asnje lojtar!");
}
else
{
updateListbox("Player: " + i + " ka te qelluara " + teQelluar + " Numra: ");
updateListbox(string.Join(", ", numrateQelluar));
}
}