猜猜秘密词C#初学者 [英] Guessing secret word C# beginner

查看:68
本文介绍了猜猜秘密词C#初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过一遍又一遍地编写这段代码,被授予初学者并没有帮助我的事业。说明是

猜猜按钮应该允许第二个用户输入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.

推荐答案

简单: count guessButton_Click的本地变量方法,所以每次调用方法时都会创建它,初始化为0,并在退出时销毁。如果你希望它持久化方法调用,它需要是一个类级变量(即在方法之外声明,前缀为 private



应该发生什么是用户单击按钮,然后检查匹配。如果匹配,则获胜。如果不匹配,则增加 count 并检查。如果它是三,他就输了。否则他又得到了一次机会。

不要在控制事件处理程序中循环;这在Windows程序中不起作用,因为用户是在控制中,你对他所做的事情作出反应。这与你在控制台应用程序中所做的非常不同,你的代码告诉用户该做什么以及何时做什么!
Simple: count is a local variable to your guessButton_Click method, so each time the method is called it is created, initialised to 0, and destroyed when you exit. If you want it to persist over method calls it needs to be a class level variable (i.e. declared outside the method a prefixed by private.

What should happen is the user clicks the button, and you check the match. If it matches, he wins. if it doesn't, increment count and check that. IF it is three, he loses. Otherwise he gets another chance.
Don't loop inside a control event handler; that doesn't work in Windows programs, as the user is in control and you react to what he does. This is very different to what you have been doing with console apps where your code tells the user what to do and when!


这篇关于猜猜秘密词C#初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆