2017-09-22 2 views
0

Mon égal si les déclarations continuent à renvoyer faux des guessances de la lettre n'est pas la première lettre, est-ce que quelqu'un a une idée pourquoi cela peut être? J'ai beaucoup débogué c'est comme ça que j'ai découvert qu'il renvoie une fausse déclaration booléenne. J'ai beaucoup googlé mais je n'ai trouvé aucun éclaircissement. Les deux premières instructions if return true si la réponse est la lengh correcteC# Egal à la déclaration 0 Faux

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); 
     } 

    } 
} 
+3

Vous n'attribuez pas le résultat 'rand.Next()' aux éléments du tableau. –

+0

'rand.Next (Min, Max)' renvoie une valeur - vous devez l'ajouter à votre tableau – thisextendsthat

+0

L'une des réponses at-elle aidé @LiamVallance? – mjwills

Répondre

0

vous n'avez pas initialiser votre tableau avec des nombres aléatoires. changer

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

à

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

OP a initialisé le tableau, mais ne l'a pas peuplé;) De plus cela ne fonctionnera pas car vous ne pouvez pas modifier la collection pendant que vous l'itérez –

+0

J'ai mis à jour ma réponse) –

+0

Merci! Je viens juste de recommencer à apprendre et j'ai commencé à apprendre les tableaux. Je me demande s'il existe un meilleur moyen de remplir les txtboxes? –

6

Vous devez sauver valeurs générées, mettre for boucle à la place 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); 
} 

ne fait pas ce vous pensez que c'est le cas.

Il ne couchait les valeurs là, il est juste itérez le tableau et générer des nombres aléatoires (et les jeter puisque vous n'êtes pas assignez rand.Next à quoi que ce soit).

Vous devez le remplacer par:

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

Votre question est parce que vous n'êtes pas assignez rand.Next() à toute valeur. Changez votre boucle foreach à cette boucle for à la place:

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

Ce que votre code actuel n'itérer à travers chaque élément dans votre tableau (6 dans ce cas), appelle rand.Next(), mais jamais l'affecte à tout élément.