[英]How do I write an if statement for my guessing game in C#
Im designing a game on visual C# the game must contain two text boxes and two bottons and a lable 我在视觉C#上设计游戏,该游戏必须包含两个文本框,两个botton和一个标签
in the first text bos I should enter the range of numbers the program should randomly generate. 在第一个文本中,我应该输入程序应随机生成的数字范围。 ---- and its activated by the botton.
----并由botton激活。
the second text box is the place where i enter the guesses ,---- also there is a botton to read the number. 第二个文本框是我输入猜测的地方,----还有一个波顿可以读取数字。
I will have a limit of ten guesses , each is assgined a color. 我最多只能猜测十个,每个都附上一种颜色。
for example the game will start with the green and if my guess was wronge the color wil change to a darker color ( dark green ,darker ..... red etc) and if I guessed the right answer the screen will get back to the green with a lable that say you won !! 例如,游戏将以绿色开始,如果我猜错了,颜色将更改为较深的颜色(深绿色,较暗的.....红色等),如果我猜对了答案,屏幕将返回到绿色与一个标签,说你赢了! or if I finished my guesse it show me Game Over !!
如果我猜对了,那就告诉我游戏结束了!
Now Im not that xpert in Visual C# so I find it hard where to write the code ... and how to activate actions as the color changing thing 现在我在Visual C#中不是那种xpert,所以我很难在哪里编写代码...以及如何激活动作来改变颜色。
BUT i understand that i need to use the random function and know how to set the range as you will see in the code below but Now Im stuck I don't know how to continue,,, Some one please guide me and help me. 但是我知道我需要使用随机函数,并且知道如何设置范围,如您在下面的代码中看到的那样,但是现在我被卡住了,我不知道如何继续,,,有人请指导我并帮助我。
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
int range;
range = int.Parse(textBox1.Text);
System.Random RandNum = new System.Random();
int Magicnumber = RandNum.Next(0, range);
int numberofguesses = 0;
}
private void button2_Click(object sender, EventArgs e)
{
int usersguess ;
usersguess = int.Parse(textBox2.Text);
if (usersguess == Magicnumber) ;
{
// I dont know what to write here
}
{
if (usersguess != Magicnumber);
{
// I dont know what to write here
}
}
}
}
} }
I know this stupid but Im not pro and I would like to learn from you guys and I belive this is the right place to ask .... 我知道这很愚蠢,但我不是专业人士,我想向你们学习,我相信这是问的正确地方。
thanx in advance 预先感谢
(This started as a comment, but nobody mentioned it, and I got carried away...) (这开始只是一个评论,但没有人提及它,我被带走了……)
Please note that adding a semicolon after the if
is a mistake (this is probably why else
didn't work for you, and you've made this strange if-if
anti pattern). 请注意,在
if
之后添加分号是一个错误(这可能就是为什么else
对您不起作用,并且您使这种奇怪的if-if
反模式)的原因。
This is the proper way to write an if
statement (else is optional, by the way): 这是编写
if
语句的正确方法(顺便说一下,else是可选的):
if(key == 'Q')
{
LaunchMissiles();
}
else
{
GivePeaceAChance();
}
Now, what you've got there is quite different: 现在,您所拥有的完全不同:
if(key == 'Q');
{
LaunchMissiles();
}
See that semicolon after the if? 看到后是否有分号? That means that
if(key=='Q');
这意味着
if(key=='Q');
and LaunchMissiles();
和
LaunchMissiles();
are two different statements , rather then a condition. 是两个不同的陈述 ,而不是一个条件。 This is the same as:
这与以下内容相同:
if(key == 'Q') DoNothing();
LaunchMissiles();
Despite it's appearance, the curly braces won't help you here - you can group a bunch of statements in curly braces, but it has no effect on flow control (unless immediately after an if, or loop, of course). 尽管有花括号,但花括号并不能为您提供帮助-您可以在花括号中组合一堆语句,但对流控制没有影响(当然,除非在if或loop之后立即执行)。
your if statement needs a tidy up for a start: 您的if语句需要整理一下:
if (usersguess == Magicnumber)
{
// I dont know what to write here
}
else
{
// I dont know what to write here
}
Hmmm this seems like a homework type question but anyway (Since you did not mention whether this is a winforms or asp.net app i am going to assume winforms) . 嗯,这似乎是一个作业类型的问题,但是无论如何(因为您没有提到这是winforms还是asp.net应用程序,因此我将假设是winforms)。
For starters you need to move the declaration of 对于初学者,您需要移动以下内容的声明:
int Magicnumber = RandNum.Next(0, range); int Magicnumber = RandNum.Next(0,范围);
and make it a member variable so that it can be accessed by all the functions in your form. 并将其设置为成员变量,以便表单中的所有函数均可对其进行访问。
Secondly in your button click you can set the back color of a button to show the colors changing or you can use images , i would go with the button since you can dynamically change its color by keeping a variable that store the hex value of a color and incrementing it on every failure eg 其次,在按钮单击中,您可以设置按钮的背景色以显示变化的颜色,也可以使用图像,因为可以通过保留存储颜色十六进制值的变量来动态更改其颜色,所以我会选择按钮并在每次失败时增加它,例如
Color myColor = Color.FromArgb(100,123,23,myvar); 颜色myColor = Color.FromArgb(100,123,23,myvar); btnColor.BackColor = myColor;
btnColor.BackColor = myColor;
By incrementing/decrementing myColor in the example above the backColor of the button will change on each failure. 通过在上面的示例中增加/减少myColor,按钮的backColor将在每次失败时更改。 If the user gets the answer right then you can set the backColor to a default color of your choice.
如果用户正确回答,则可以将backColor设置为您选择的默认颜色。
First of all, here is the better way to write your 'if' construction: 首先,这是编写“ if”构造的更好方法:
if (int.Parse(textBox2.Text) == Magicnumber)
{
...
}
else
{
...
}
If you want to count the number of user attempts to guess the number, you should store it in a variable. 如果要计算用户尝试猜测该数字的次数,则应将其存储在变量中。 And this variable should be reset to zero any time you start new game.
每当您开始新游戏时,此变量都应重置为零。
private int count = 0;
private void button2_Click(object sender, EventArgs e)
{
...
if (int.Parse(textBox2.Text) == Magicnumber)
{
// User wins
...
}
else
{
// Wrong answer
count++;
...
}
}
Then, if you want to change the background color of some WinForms control, use BackColor property (its value should depend of count). 然后,如果要更改某些WinForms控件的背景色,请使用BackColor属性(其值应取决于计数)。 This example changes background color of TextEdit instance:
本示例更改TextEdit实例的背景颜色:
textEdit1.BackColor = Color.DarkGreen;
The Color here is System.Drawing.color class that may represent any color and have a lot of named values ( http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx , http://msdn.microsoft.com/en-us/library/aa358802%28VS.85%29.aspx ). 颜色在这里是可表示任何颜色,有很多命名值的System.Drawing.Color类( http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx , HTTP:/ /msdn.microsoft.com/zh-CN/library/aa358802%28VS.85%29.aspx )。 I strictly recomed you to begin with reading some good book on C# before trying to solve any programming tasks.
我严格建议您在尝试解决任何编程任务之前先阅读一些有关C#的好书。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.