需要帮助C#Program [英] Need help with C# Program

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

问题描述

我的代码不会转到游戏的下一个随机单词,我是C#的新手并且无法解决这个问题。我缺少什么?



my code will not go to the next random word for the game, I am new to C# and cant figure this out. What am I missing?

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 NewWordScramble
{
    struct WordScramble
    {
        public string Word;
    }

    public partial class Form1 : Form
    {
        private string strstoreword;
        private string word;
        Random r = new Random();
        
        public Form1()
        {
            InitializeComponent();
            
            timer1.Enabled = false;
         }

        private void ReadFile()
        {
            try
            {
              StreamReader inputFile;
              string wordline;
        
              WordScramble wordlist = new WordScramble();

                char[] delim = { ' ' };

                inputFile = File.OpenText("WordScrambleListtxt.txt");

               while (!inputFile.EndOfStream)
                {
                    wordline = inputFile.ReadLine();

                    string[] tokens = wordline.Split(delim);

                    wordlist.Word = tokens[0];

                    WordTextBox.Text = wordlist.Word;                   
                }
              // inputFile.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        string DisplayWords()
        {
            string word = WordTextBox.Text;
            Random r = new Random();
            String[] myString = File.ReadAllLines("WordScrambleListtxt.txt");

             string scrambled_Word = ScrambleWord(word);
             WordTextBox.Text = scrambled_Word;
             strstoreword = word;
                       
            return myString[r.Next(0, myString.Length - 1)];
          
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            ReadFile();
               DisplayWords();
        }

        public string ScrambleWord(string word)
        {
            char[] chars = new char[word.Length];
            Random rand = new Random(10000);
            int index = 0;
            while (word.Length > 0)
            {
                int next = rand.Next(0, word.Length - 1);
                chars[index] = word[next];
                word = word.Substring(0, next) + word.Substring(next + 1);
                ++index;
            }
                return new String(chars);
        }

        private void CheckWordButton_Click(object sender, EventArgs e)
        {
            string strGuess;
            strGuess = txtGuess.Text.ToString();

            if (strGuess.ToLower() == strstoreword)
            {
                lblResult.Text = "You are CORRECT!";
                timer1.Enabled = true;
            }
            else
            {
                lblResult.Text = "Wrong! Please try again!";
                txtGuess.Clear();
            }

            txtGuess.Focus();
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (lblResult.BackColor == Color.Navy)
            {
                lblResult.BackColor = Color.Red;
            }
            else
            {
                lblResult.BackColor = Color.Navy;
            }
        }

        private void PlayAgainButton_Click(object sender, EventArgs e)
        {
            txtGuess.Clear();
            WordTextBox.Clear();
            ReadFile();
            DisplayWords();
            timer1.Enabled = false;
            lblResult.Text.Remove(0);
            lblResult.Text = "";

            ReadFile();
            DisplayWords();
        }
    }
}

推荐答案

我在这里至少可以看到一个错误。只创建一次random实例,不要重新创建它,使用相同的实例。并使用一些种子:

https:/ /msdn.microsoft.com/en-us/library/ctssatww%28v=vs.110%29.aspx [ ^ ]。



小码本MSDN文章末尾的示例显示了如何根据 System.DateTime 制作种子。







另见这个答案的评论。你不应该使用常量来播种(参见 rand ),你不需要三个随机的实例(或者如何你有很多)。这根本不是好代码。只是把它整理好,我们可以讨论问题,你还需要解释......



-SA
I can see at least one mistake here. Create an instance of random only once, don't re-create it, use the same instance. And use some seed:
https://msdn.microsoft.com/en-us/library/ctssatww%28v=vs.110%29.aspx[^].

The small code sample at the end of this MSDN article shows how to make a seed based on System.DateTime.



See also comments to this answers. You should not use constant for seeding (see rand), you don't need three instances of Random (or how many do you have). This is not good code at all. Just put it in order, than we can discuss problems, which you still have to explain…

—SA


这篇关于需要帮助C#Program的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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