问题描述
我已经尝试过一遍又一遍地编写这段代码,被授予初学者并没有帮助我的事业。说明是
猜猜按钮应该允许第二个用户输入3个猜测
a。在列表框中显示每个猜测
b。如果猜测错误,请显示Keep trying message&保持循环直到3次尝试
c。如果猜测正确,请显示祝贺信息&停止循环
下面我编写的代码一直在失败。感谢帮助!
使用System;
使用System.Collections.Generic;
使用System.ComponentModel ;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System .Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间猜猜
{
公共部分类Form1:表格
{
//声明并创建InputForm对象
InputForm inputForm = new InputForm();
//在类级别声明秘密单词,以便可以共享
//由所有点击事件处理程序 - 不要重新声明它
string secret =;
public Form1()
{
InitializeComponent();
}
private void secretButton_Click(object发件人,偶数tArgs e)
{
//此代码显示如何使用输入法
//字符串密码已在上面声明
//输入密码
secret = inputForm.GetData(输入密码);
}
private void guessButton_Click(object sender,EventArgs e)
{
string猜测; //猜猜词
int count = 0; //猜测数量
//为GUESS按钮编写代码的剩余部分
//猜猜密码
guess = inputForm。 GetData(猜猜秘密词。);
//添加猜测到列表框
guessListBox.Items.Add(guess) ;
if(guess!= secret)
{
MessageBox.Show(继续尝试) ;
while(count< = 3)
{
//添加一个循环计数器
count = count + 1;
}
}
else
{
MessageBox.Show(祝贺);
}
}
private void clearButton_Click(object sender,EventArgs e)
{
//清除列表框
guessListBox.Items.Clear();
}
private void exitButton_Click(object sender,EventArgs e)
{
//关闭程序
this.Close();
}
}
}
我尝试了什么:
我尝试过以7种不同的方式编写代码,这是最接近的代码。
I have tried writing this code over and over, granted being a beginner isn't helping my cause. The instructions are
Guess button should allow second user to enter 3 guesses
a. Display each guess in the list box
b. If the guess is wrong, display a Keep trying message & keep looping until 3 tries
c. if guess is correct, display a congratulations message & stop loop
The code I have written that keeps failing is below. Help is appreciated!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Guessing
{
public partial class Form1 : Form
{
// Declare and create InputForm object
InputForm inputForm = new InputForm();
// Declare secret word at class level so it can be shared
// by all click event handlers - Do not re-declare it
string secret = "";
public Form1()
{
InitializeComponent();
}
private void secretButton_Click(object sender, EventArgs e)
{
// THIS CODE SHOWS HOW TO USE THE INPUTFORM
// string secret has already been declared above
// Input secret word
secret = inputForm.GetData("Enter secret word");
}
private void guessButton_Click(object sender, EventArgs e)
{
string guess; //Guess word
int count = 0; //Amount of guesses
// WRITE THE REST OF THE CODE FOR THE GUESS BUTTON
//Guess secret word
guess = inputForm.GetData("Guess the secret word.");
//Add guess to list box
guessListBox.Items.Add(guess);
if (guess != secret)
{
MessageBox.Show("Keep trying");
while (count <= 3)
{
//Add one to loop counter
count = count + 1;
}
}
else
{
MessageBox.Show("Congratulations");
}
}
private void clearButton_Click(object sender, EventArgs e)
{
//Clear list box
guessListBox.Items.Clear();
}
private void exitButton_Click(object sender, EventArgs e)
{
//Close program
this.Close();
}
}
}
What I have tried:
I have tried writing the code about 7 different ways and this is the one that comes out closest.
推荐答案
这篇关于猜猜秘密词C#初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!