|
|
|
| |||||||||
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#16
|
||||
|
||||
|
Be proud of yourself it looks good so far.
To answer your question: You want your end loop condition to be what you're using to see if it's right. So the loop ends when the input is right. Code:
boolean right = false;
while (right) {
//enter word1:
if (word1.length() <= 37) right = true;
}
__________________
The day I get my hands on the cookbook it's all over. -nicky |
|
#17
|
||||
|
||||
|
Quote:
True, I didn't catch that. Sorry for the mistake. |
|
#18
|
||||
|
||||
|
Something else I noticed:
You've got a stray while statement at the end there. Were you going for a do while loop? If so it's really not necessary. |
|
#19
|
|||||||
|
|||||||
|
Quote:
It's never too soon to talk about design and/or refactoring. I generally like to break methods up into fairly atomic units of work. Again, once you have a decent algorithm, the code practically writes itself. Here's an example: Pseudocode: 1) Ask the user for a word 2) Ask the user for another word 3) Display the words That's the basic idea of the program. We'll see that each step has its own set of requirements, but let's just start with this algorithm of program flow. Okay, this obviously won't compile (the methods aren't defined), but I'm not too worried about it. This pretty much covers the main program, right? Now, we just need to define those methods. We'll write some basic methods as "stubs", just to get a working program... Okay, this compiles and runs, so we can see that we've got a working skeleton. Let's tackle the next small piece: user input. Once again, let's write up our algorithm in pseudocode: 1) Get some input from the user 2) While the input is invalid 2a) Notify user of the error 2b) Try to get good input Translated to Java, we get the following for our getWordFromUser() method: Again, we've got more undefined methods, so let's stub it out... So now, we've got a compiling and running program... Java Code:
So now, we can go in and handle each small stub... Java Code:
You'll notice the MAX_INPUT_LENGTH variable. It's a constant I chose to define because I loathe Magic Numbers. So once I define all that, I've got another compilable, runnable class I can test.... |