How about this?
// GuessTheNumberTest.java
//Written by Sean Kelley for IS109-8b
//Week 3 homework
//Test the game GuessTheNumber
import java.util.Scanner;//uses Scanner
public class GuessTheNumberTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);//initializes Scanner
do {//begin while to play again
(new GuessTheNumber()).play();//call method play
System.out.println("Play again? 1 for yes or 2 for no:");//prompt for input
} while (1 == input.nextInt());
System.out.println("Thanks For Playing!");//output end message
}//end main
}//end class GuessTheNumberTest
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
...and this:
// GuessTheNumber.java
//Written by Sean Kelley for IS109-8b
//Week 3 homework
import java.util.Random;//uses Random
import java.util.Scanner;// uses Scanner
public class GuessTheNumber {
//method to facilitate the guess process
public void guess( int number )
{
int guessedNum;// user input guess
Scanner input = new Scanner( System.in );
System.out.println("Guess a number between 1 and 1000");//prompt for input
while(number != (guessedNum = input.nextInt()))
{// display messages to help user zero in on number
if ( guessedNum < number )
{
System.out.println("Too low. Try again.");
continue;
}//end if
System.out.println("Too high. Try again.");
}//end While
//display winner message
System.out.println("Congratulations! You guessed the Number!");
}//end method guess
//method to tie it all togeather
public void play(){
guess(1 + (new Random().nextInt(1000)));
}//end play
}//end class GuessTheNumber
Also: there are too many comments.
The comments should be removed from things that are obvious.
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
@hfx642
Personally, I think obvious lines that don't require comments are:
>> import java.util.Random;//uses Random
>> import java.util.Scanner;// uses Scanner
>> //display winner message
>> guess = input.nextInt();//saves input as guess
...and a lot of variables (from us all) can be renamed so that comments are not necessary, like:
int intNumUserGuessed = 0;
[From Uncle Bob Martin's "Clean Code"]
http://www.informit.com/articles/article.aspx?p=1327761
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
Question Answered as of 1 Year Ago by
thines01,
hfx642,
frank33
and 1 other