Hello, this is my first post. I have this homework assignment, and it is completed and it works! my question is just if anyone has any suggestions to clean it up or make it more efficient? I have two seperate files GuessTheNumber.java which is:
// 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 {
private int number;//random number to search for
private int guess;// user input guess
//method go get Random number
public int pickNum(){
Random randNum = new Random();
number = 1 + randNum.nextInt(1000);//uses random to get a Number between
// 1 and 1000 and saves as Number
return number;//returns number
}//end pickNum
//method to facilitate the guess process
public void guess( int number ){
int max = 1000;// max starts at 1000
int min = 1;// min starts at 1
Scanner input = new Scanner( System.in );
System.out.println("Guess a number between 1 and 1000");//prompt for input
guess = input.nextInt();//saves input as guess
while( guess != number ){// display messages to help user zero in on number
if ( guess < number ){
min = guess;
System.out.println("Too low. Try again.");
guess = input.nextInt();
}//end if
else{
max = guess;
System.out.println("Too high. Try again.");
guess = input.nextInt();
}// end else
}//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( pickNum());
}//end play
}//end class GuessTheNumber
and then there is the GuessTheNumberTest.java which is:
// 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){
int again;//initializes again
Scanner input = new Scanner(System.in);//initializes Scanner
GuessTheNumber guessTheNumber = new GuessTheNumber();
guessTheNumber.play();//Calls method play
System.out.println("Play again? 1 for yes or 2 for no:");//prompt for input
again = input.nextInt(); // save input as again
while ( again == 1 ){//begin while to play again
guessTheNumber.play();//call method play
System.out.println("Play again? 1 for yes or 2 for no:");//prompt for input
again = input.nextInt();//save input as again
}//end While
System.out.println("Thanks For Playing!");//output end message
}//end main
}//end class GuessTheNumberTest
like I said it is complete just want to see if anyone has any comments or suggestions.
Thanks.
JavaNewb