C#Equals语句返回False
如果语句不断返回false,那么猜测不是第一个字母,有人知道这是为什么吗?我已经调试了很多次,所以我发现它返回了一个错误的布尔语句。我在谷歌上搜索了很多,但没有发现什么启示。如果答案是正确的长度,则前两个if语句返回trueC#Equals语句返回False,c#,loops,random,foreach,equals,C#,Loops,Random,Foreach,Equals,如果语句不断返回false,那么猜测不是第一个字母,有人知道这是为什么吗?我已经调试了很多次,所以我发现它返回了一个错误的布尔语句。我在谷歌上搜索了很多,但没有发现什么启示。如果答案是正确的长度,则前两个if语句返回true using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Li
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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
名称空间猜测单词
{
公共部分类Form1:Form
{
私有int错误猜测=0;
私有int用户猜测;
私人智力得分=0;
私有字符串secretWord=string.Empty;
私有字符串[]字;
私有字符串currentWord=string.Empty;
私有字符串userGuess=string.Empty;
私有字符串userInput=string.Empty;
私有字符串randomInput=string.Empty;
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有无效猜测单击(对象发送者,事件参数e)
{
char[]userInputArray=userInputBox.Text.ToLowerInvariant().ToCharArray();
char[]currentWordCharArray=currentWord.ToLowerInvariant().ToCharArray();
//假设userInput永远不会优于RandomCharray
//并且只包含一个字符
for(int i=0;i0&&userInputArray.Length>i)
如果(CurrentWordCharray.Length>0&&CurrentWordCharray.Length>i)
if(userInputArray[0].Equals(currentWordCharArray[i]))
{
UpdateScore();
}
}
//清除表单中的用户输入
userInputBox.Text=string.Empty;
}
私有void resetGamebtn_单击(对象发送者,事件参数e)
{
词组();
}
专用词汇()
{
字符串路径=(@“C:\commonwords.txt”);//将变量路径与txt文件的路径一起保存
words=File.ReadAllLines(路径);
int guessIndex=(new Random()).Next(words.Length);
currentWord=单词[猜测索引];
wordlbl.Text=string.Empty;
for(int i=0;i
您必须保存生成的值,将放入循环,而不是foreach
:
for (int i = 0; i < randomArray.Length; ++i)
randomArray[i] = rand.Next(Min, Max);
for(int i=0;i
不是在做你认为它能做的事
它不是设置那里的值,它只是在数组上迭代并生成一些随机数(并且丢弃它们,因为您没有将rand.Next
赋值给任何对象)
您需要将其替换为:
for (var i=0; i < randomArray.Length; i++)
{
randomArray[i] = rand.Next(Min, Max);
}
for(var i=0;i
您没有使用随机数初始化数组。
改变
到
for(var i=0;i
您的问题是因为您没有将rand.Next()
赋值给任何值。将您的foreach
循环改为此for
循环:
for (int i=0;i<6;i++)
{
randomArray[i] = rand.Next(Min, Max);
}
for(int i=0;i您没有将rand.Next()
result赋值给数组的元素。rand.Next(Min,Max)
返回一个值-您需要将它添加到数组ID任何答案help@LiamVallance?OP确实初始化了数组,但没有填充它;)此外,这将不起作用,因为您不能在迭代集合时修改集合谢谢!我刚开始学习,开始学习数组。。我想知道有没有更好的办法填满TXT盒子?
foreach (int value in randomArray)
{
rand.Next(Min, Max);
}
for (var i=0; i < randomArray.Length; i++) {
randomArray[i] = rand.Next(Min, Max);
}
for (int i=0;i<6;i++)
{
randomArray[i] = rand.Next(Min, Max);
}