【问题标题】:C# Equals Statement Returning FalseC# Equals 语句返回 False
【发布时间】:2017-09-22 13:23:49
【问题描述】:

我的equals if语句不断返回错误的字母猜测不是第一个字母,有人知道为什么会这样吗?我调试了很多,这就是我发现它返回一个错误的布尔语句的方式。我用谷歌搜索了很多,但我没有发现任何启示。如果答案是正确的长度,则前两个 if 语句返回 true

    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;
using System.IO;

namespace Guess_The_Word
{

    public partial class Form1 : Form
    {
        private int wrongGuesses = 0;
        private int userGuesses;
        private int score = 0;
        private string secretWord = String.Empty;
        private string[] words;
        private string currentWord = string.Empty;
        private string userGuess = string.Empty;
        private string userInput = string.Empty;
        private string randomInput = string.Empty;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {



        }

        private void guessBtn_Click(object sender, EventArgs e)
        {
            char[] userInputArray = userInputBox.Text.ToLowerInvariant().ToCharArray();
            char[] currentWordCharArray = currentWord.ToLowerInvariant().ToCharArray();
            //Assume that userInput would never be superior than randomCharArray
            //And contain only one char
            for (int i = 0; i < currentWordCharArray.Length; i++)
            {
                if (userInputArray.Length > 0 && userInputArray.Length > i)
                    if (currentWordCharArray.Length > 0 && currentWordCharArray.Length > i)
                        if (userInputArray[0].Equals(currentWordCharArray[i]))

                {
                    UpdateScore();
                }
            }
            // Clean userInput in form
            userInputBox.Text = string.Empty;

        }


        private void resetGamebtn_Click(object sender, EventArgs e)
        {
            SetUpWords();


        }

        private void SetUpWords()
        {
            string path = (@"C:\commonwords.txt"); // Save the variable path with the path to the txt file
            words = File.ReadAllLines(path);
            int guessIndex = (new Random()).Next(words.Length);
            currentWord = words[guessIndex];
            wordlbl.Text = string.Empty;
            for (int i = 0; i < currentWord.Length; i++)
            {

                wordlbl.Text += "*";

            }
        }

        private void userInputBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void UpdateScore()
        {
            score++;
            scorelbl.Text = Convert.ToString(score);
        }

    }
}

【问题讨论】:

  • 您没有将 rand.Next() 结果分配给数组的元素。
  • rand.Next(Min, Max) 返回一个值 - 你需要将它添加到你的数组中
  • 任何答案对@LiamVallance 有帮助吗?

标签: c# loops random foreach equals


【解决方案1】:

你必须保存生成的值,把for循环而不是foreach

 for (int i = 0; i < randomArray.Length; ++i)
    randomArray[i] = rand.Next(Min, Max);

【讨论】:

    【解决方案2】:
    foreach (int value in randomArray)
    {
        rand.Next(Min, Max);
    }
    

    没有做你认为的事情。

    它不是设置那里的值,它只是遍历数组并生成一些随机数(并丢弃它们,因为您没有将rand.Next 分配给任何东西)。

    您需要将其替换为:

    for (var i=0; i < randomArray.Length; i++)
    {
        randomArray[i] = rand.Next(Min, Max);
    }
    

    【讨论】:

      【解决方案3】:

      你没有用随机数初始化你的数组。 改变

      foreach (int value in randomArray)
          {
              rand.Next(Min, Max);
          }
      

      for (var i=0; i < randomArray.Length; i++) {
          randomArray[i] = rand.Next(Min, Max);
      }
      

      【讨论】:

      • OP 确实初始化了数组,但没有填充它;)此外,这不起作用,因为您无法在迭代时修改集合
      • 谢谢!我刚刚回到学习并开始学习数组。我想知道有没有更好的方法来填充 txtboxes?
      【解决方案4】:

      您的问题是因为您没有将 rand.Next() 分配给任何值。将您的 foreach 循环改为此 for 循环:

      for (int i=0;i<6;i++)
      {
          randomArray[i] = rand.Next(Min, Max);
      }
      

      您当前的代码所做的是遍历数组中的每个元素(在本例中为 6 个),调用 rand.Next(),但从不将其分配给任何元素。

      【讨论】:

        猜你喜欢
        • 2019-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-11
        • 2011-04-12
        • 2020-08-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多