| | |
Need help on exiting while loop
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
![]() |
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
Hi! I'm having trouble solving this problem. Since it's finals week here at my school the Computer science tutors left early and I need just this simple problem finished by tomorrow @ noon.
I'm working on the hangman game as my final project. I've got everything done, except for this silly thing. I'm trying to detect if a user entered a letter already, and if they did, loop the input until they enter a letter that's not in this string array. Everything is working except that I can't exit this loop! Here's my code:
Could someone please help my find my problem?! I don't know why it's not exiting the while loop. Whenever the boolean contains is false, it's supposed to exit the loop, but it's not. Someone please help! Thanks!
I'm working on the hangman game as my final project. I've got everything done, except for this silly thing. I'm trying to detect if a user entered a letter already, and if they did, loop the input until they enter a letter that's not in this string array. Everything is working except that I can't exit this loop! Here's my code:
while (contains == true) {
if (contains = true) {
userInput = JOptionPane.showInputDialog(null,"This letter has already been entered! Please enter a letter");
c = userInput.charAt(0);
b++;
for (int u=0;u<lettersEntered.length;u++) {
if (lettersEntered[u] == c) {
contains = true;
}
}
}
else {
lettersEntered[g]=c;
System.out.println(lettersEntered[g]);
contains = false;
}
}Could someone please help my find my problem?! I don't know why it's not exiting the while loop. Whenever the boolean contains is false, it's supposed to exit the loop, but it's not. Someone please help! Thanks!
Last edited by vertigovirgo; Dec 17th, 2008 at 12:12 am.
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
What's the difference between = and ==
Where are you using one, where you should be using the other?
Well, from my CS tutors, the difference is == is that it's comparing the boolean to see if it's true, while just an = is setting the boolean. So in the while/if/for loops, I want to compare to see if it's true, not set it.
Btw I've been just completely trying to get this answer right but after a few reviews, I've concluded that my logic is completely off (the while loop is only comparing the one element in the boolean array)... sad thing is I don't know where to start. Could I get some help on garnering ideas? I completely didn't come here for quick answers on this. I just don't know where to start, and some ideas could help. My idea for this loop was to do the following:
-Check lettersEntered array to see if letter is in array
-If it isn't, add that letter into the array
-if it is, prompt user to enter another letter
-keep prompting until letter is not found in loop
-set a boolean condition to false, which will exit the loop
But my logic is not working, and my compiler (stupid DrJava) isn't doing these steps.
Last edited by vertigovirgo; Dec 17th, 2008 at 2:47 am. Reason: adding more details
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
OK, let me be blunt.
There's one place where you're using = where you should be using ==
You use == correctly in a similar place.
Here's my updated (but still not working) code:
boolean contains = false;
for (int we=0;we<lettersEntered.length;we++){
if (lettersEntered[we] == c){
contains = true;
}
else {
lettersEntered[g]=c;
}
}
while (contains == true) {
userInput = JOptionPane.showInputDialog(null,"This letter has already been entered! Please enter a letter");
c = userInput.charAt(0);
for (int u=0;u<lettersEntered.length;u++) {
if (lettersEntered[u] != c) {
lettersEntered[g]=c;
contains = false;
}
}
}The problem is, I can't find a way to set contains to false or true only one time... I want to loop through the lettersEntered array to find out if any letter has already been entered. The problem is with the lettersEntered array..
Last edited by vertigovirgo; Dec 17th, 2008 at 3:09 am.
The problem is that you are mixing two booleans.
First: make a boolean function to check if a letter is in an array or not.
Second: decide with a boolean when you want to end your while loop.
First: make a boolean function to check if a letter is in an array or not.
Second: decide with a boolean when you want to end your while loop.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- Code for Exiting a C# Application (C#)
- Need help reading a txt file into C++ (C++)
- problem with loop (C)
- exiting loop with switch (C++)
- New Python Problem (Python)
- help with parrallel arrays (C++)
- C - Getting double output? (C)
Other Threads in the Computer Science Forum
- Previous Thread: I need some tips on creating a flowchart.
- Next Thread: Free Ideas for Thesis
| Thread Tools | Search this Thread |
ai algorithm algorithms amazon assignment assignmenthelp assignments automata battery bigbrother binary bittorrent bizarre bletchleypark blogging bomb business cern codebreaker compiler computer computers computerscience computertrackingsoftware conversion csc data dataanalysis dataintepretation development dfa dissertation dissertations dissertationthesis dissertationtopic ebook employment energy extensions floatingpoint foreclosuresoftware fuel gadgets geeks givemetehcodez government graphics hardware history homeowners homeworkassignment homeworkhelp humor ibm idea ideas internet iphone ipod itcontracts jobs kindle laser laws lsmeans mainframes marketing mining mobileapplication msaccess netbeans networking news os p2p piracy piratebay programming research sas science security sex simulation software spying sql study supercomputer supercomputing sweden technology textfield turing turingtest two'scompliment uk virus warehouse ww2






