C#刽子手游戏:不根据后续用户猜测进行更新
我创建了一个非常简单的刽子手游戏,让用户输入一个字母作为猜测。当用户第一次输入正确的猜测时,游戏会记录下来,并且控制台会显示正确的字母。但是,对于每个后续的正确猜测,我为解决方案创建的数组显然没有更新,控制台也没有编写第二个正确猜测。此外,用户不会因为每次错误的猜测而失去生命 谁能解释一下我哪里出了问题C#刽子手游戏:不根据后续用户猜测进行更新,c#,C#,我创建了一个非常简单的刽子手游戏,让用户输入一个字母作为猜测。当用户第一次输入正确的猜测时,游戏会记录下来,并且控制台会显示正确的字母。但是,对于每个后续的正确猜测,我为解决方案创建的数组显然没有更新,控制台也没有编写第二个正确猜测。此外,用户不会因为每次错误的猜测而失去生命 谁能解释一下我哪里出了问题 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Hangman2
{
class Program
{
static string[]letters = {"a","e","r","o","p","l","a","n","e"};
static int Lives = 7;
static string Gu = ""; // this is not being updated with each guess
static string[]Solution = new string[letters.Length];
public static void Main(string[] args)
{
FillSolution();
UserGuess();
while (Lives > 0) {
UserGuess();
}
Console.ReadLine();
}
static void FillSolution()
{
string star = "*";
for(int i = 0; i < Solution.Length; i++) {
Solution[i] = star;
}
}
static void UserGuess()
{
Console.WriteLine("Please enter your guess");
string Guess = Console.ReadLine();
Gu += Guess; //this isn't working
bool GuessRight = false;
for(int i = 0; i<letters.Length; i++) {
string y = letters[i];
if(Gu == y) {
Solution[i] = letters[i];
GuessRight = true;
}
else {
GuessRight = false;
}
if(GuessRight = false) {
Lives--; // this is also not working
}
}
DisplaySolution();
}
static void DisplaySolution()
{
for(int i = 0; i < Solution.Length; i++) {
Console.Write(Solution[i]);
}
Console.WriteLine("You have " + Lives + " lives remaining");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
使用System.Threading.Tasks;
名称空间刽子手2
{
班级计划
{
静态字符串[]字母={“a”、“e”、“r”、“o”、“p”、“l”、“a”、“n”、“e”};
静态寿命=7;
静态字符串Gu=“”;//这不会随着每次猜测而更新
静态字符串[]解决方案=新字符串[letters.Length];
公共静态void Main(字符串[]args)
{
填充溶液();
UserGuess();
而(寿命>0){
UserGuess();
}
Console.ReadLine();
}
静态空隙填充溶液()
{
字符串星=“*”;
for(int i=0;i 对于(int i=0;i您的if语句使用赋值运算符,而不是检查它是否等于false
if(GuessRight = false){
应该是
if(GuessRight == false){
或者只是
if(!GuessRight){
此外,当前检查的整个if语句都是错误的,您需要找出解决方案中是否有猜测,而不是串接到Gu
字符串(这是不需要的)
if语句使用赋值运算符,而不是检查它是否等于false
if(GuessRight = false){
应该是
if(GuessRight == false){
或者只是
if(!GuessRight){
此外,当前检查的整个if语句都是错误的,您需要找出解决方案中是否有猜测,而不是串接到Gu
字符串(这是不需要的)
我注意到的第一个问题是在第72行,你写道:
if (GuessRight = false)
出现问题的原因是,您试图将GuessRight变量的值设置为false(您之前已经这样做了),而您要检查的是,GuessRight
是否等于false,并使用“==
”作为相等运算符。该行应为:
if (GuessRight == false)
我注意到的第一个问题是在第72行,你写道:
if (GuessRight = false)
出现问题的原因是,您试图将GuessRight变量的值设置为false(您之前已经这样做了),而您要检查的是,GuessRight
是否等于false,并使用“==
”作为相等运算符。该行应为:
if (GuessRight == false)
下面的内容应该可以解决您的一些生命计数问题。我不确定为什么字符串连接目前不适用于您
static void UserGuess()
{
Console.WriteLine("Please enter your guess");
string Guess = Console.ReadLine();
Gu += Guess; // this should work
bool GuessRight = false;
for(int i = 0; i<letters.Length; i++){
string y = letters[i];
if(Gu == y) {
Solution[i] = letters[i];
GuessRight = true;
}
else {
GuessRight = false;
}
if(GuessRight == false) {// Have to use '==' when comparing, '=' will assign the value
Lives--;
}
}
DisplaySolution();
}
static void UserGuess()
{
Console.WriteLine(“请输入您的猜测”);
字符串Guess=Console.ReadLine();
Gu+=Guess;//这应该行得通
bool-GuessRight=false;
对于(int i=0;i以下内容应该可以解决您的一些生命计数问题。我不确定为什么字符串串联目前不适用于您
static void UserGuess()
{
Console.WriteLine("Please enter your guess");
string Guess = Console.ReadLine();
Gu += Guess; // this should work
bool GuessRight = false;
for(int i = 0; i<letters.Length; i++){
string y = letters[i];
if(Gu == y) {
Solution[i] = letters[i];
GuessRight = true;
}
else {
GuessRight = false;
}
if(GuessRight == false) {// Have to use '==' when comparing, '=' will assign the value
Lives--;
}
}
DisplaySolution();
}
static void UserGuess()
{
Console.WriteLine(“请输入您的猜测”);
字符串Guess=Console.ReadLine();
Gu+=Guess;//这应该行得通
bool-GuessRight=false;
对于(inti=0;iSo),我错误地将第一个“for”循环转到第78行。
谢谢大家的回复
bool GuessRight = false;
for(int i = 0; i<letters.Length; i++){
if(Guess == letters[i]){
Solution[i] = letters[i];
GuessRight = true;
}
} // this was the right place to close it, not after the 'Lives--'
if(!GuessRight){
Lives--;
}
bool GuessRight=false;
对于(inti=0;iSo),我错误地将第一个“for”循环转到第78行。
谢谢大家的回复
bool GuessRight = false;
for(int i = 0; i<letters.Length; i++){
if(Guess == letters[i]){
Solution[i] = letters[i];
GuessRight = true;
}
} // this was the right place to close it, not after the 'Lives--'
if(!GuessRight){
Lives--;
}
bool GuessRight=false;
对于(int i=0;iI建议您熟悉调试器。您可以在执行此操作时逐步浏览代码并查看每个变量的值,这将有助于确定出现了什么问题。我假设您使用的是具有该功能的IDE,如VS。我建议您熟悉调试器。你可以一步一步地浏览代码并查看每个变量的值,这将有助于你确定出哪里出了问题。我假设你使用的是一个像VS这样的IDE,当然它有这样的功能。他们有else{}
,你有else()
,但是如果()
编译else不包含逻辑条件。他们实际上是在尝试分配GuessRight的值。else{GuessRight=false;}
@flamewave000-很好的地方,格式把我甩了他们有else{}
,你有else()
,但如果()
编译else不包含逻辑条件。他们实际上是在尝试分配GuessRight的值。
else{GuessRight=false;}
@flamewave000-很好,格式让我大吃一惊谢谢!还有@Sayse&Harry指出了同样的问题,当我输入正确的猜测时,我得到了(Lives=0),当我输入一个不正确的猜测时,我得到的结果是(Lives=-2)。因此,一个不正确的猜测会导致数组中的元素数量减少寿命…你需要在Lives--;
之后添加一个中断;
谢谢!@Sayse&Harry指出了同样的问题,当我输入一个正确的猜测时,我得到的结果是(Lives=0),当我输入一个不正确的猜测时,我得到的结果是(lifes=-2)