This code is working well, I am just fairly new to programming and am hoping someone more experienced can look it over and give me some pointers.
* Author: Tyler Knight
* Date Created: 02/07/2018
* Program Purpose: Guessing game using
* random numbers.
**/
//Importing random and scanner classes
import java.util.Random;
import java.util.Scanner;
public class ElusiveNumbers {
public static void main(String[] args) {
//Declare variables
Random num = new Random();
int userInput;
int answer;
int count;
int attempts;
int restart;
int terminate;
int range;
Scanner keyboard = new Scanner(System.in);
//Whitespace
System.out.println();
System.out.println();
try {
//Greeting Message
System.out.println("Welcome To Elusive Numbers!");
//Whitespace
System.out.println();
//Prompt user to enter number range
System.out.println("Enter the range of numbers you would like to guess from. EX: 5, 10, 100, 1000");
//Whitespace
System.out.println();
//Allows User To Enter Range
range = keyboard.nextInt();
//Whitespace
System.out.println();
//Prompt user to enter in however many chances they would like to have
System.out.println("Enter the amount of attempts you would like for this challenge");
//Whitespace
System.out.println();
//Allows user to set amount of attempts
attempts = keyboard.nextInt() - 1;
//Whitespace
System.out.println();
//Prompt user to guess the number
System.out.println("Enter a number between 1 and " + range);
//Whitespace
System.out.println();
System.out.println();
//Assign value to variables
answer = num.nextInt(range) + 1;
count = 0;
/*
* While the user input is not equal to the random generated number,
* it will tell the user if their number was too high or too low,
* it will also add 1 to the count variable every time user is wrong,
* if the count variable becomes equal to 3 "Game Over" will display,
* the user will then have the chance to try again or exit the game
*/
do {
//Assign value to variables
userInput = keyboard.nextInt();
restart = 1;
terminate = 0;
//If the count is higher than the attemps and user has not guessed the number, Game Over
if (count >= attempts && userInput != answer) {
//If user guess is too low, output Too Low!
if (userInput < answer) {
//Whitespace
System.out.println();
System.out.println("Too Low!");
}
//If user guess is too high, output Too High!
else if (userInput > answer) {
//Whitespace
System.out.println();
System.out.println("Too High!");
}
//Whitespace
System.out.println();
System.out.println("Game Over!");
//Whitespace
System.out.println();
System.out.println("To try again, type 1, to quit type 0");
//Whitespace
System.out.println();
userInput = keyboard.nextInt();
//Whitespace
System.out.println();
//If the user Chooses to restart the game will reset, Else the program will exit
if (userInput == restart) {
count = 0;
answer = num.nextInt(range) + 1;
System.out.println("Enter the range of numbers you would like to guess from. EX: 5, 10, 100, 1000");
//Whitespace
System.out.println();
range = keyboard.nextInt();
//Whitespace
System.out.println();
System.out.println("Enter the amount of attempts you would like for this challenge");
//Whitespace
System.out.println();
attempts = keyboard.nextInt() - 1;
//Whitespace
System.out.println();
System.out.println("Enter a number between 1 and " + range + "\t");
//Whitespace
System.out.println();
}
else if (userInput == terminate) {
return;
}
}
// If the counter is at it's limit but the answer is correct it will not go to game over
else if (count == 2 && userInput == answer) {
System.out.println("Great Job!");
System.out.println("To try again type 1, to quit type 0");
userInput = keyboard.nextInt();
//If user chooses to restart, everything will reset and game will start over
if (userInput == restart) {
count = 0; // Resets count
answer = num.nextInt(range) + 1; // Resets random number
System.out.println("Enter the range of numbers you would like to guess from. EX: 5, 10, 100, 1000");
//Whitespace
System.out.println();
range = keyboard.nextInt();
//Whitespace
System.out.println();
System.out.println("Enter the amount of attempts you would like for this challenge");
//Whitespace
System.out.println();
attempts = keyboard.nextInt() - 1;
System.out.println("Enter a number between 1 and " + range + "\t");
//Whitespace
System.out.println();
}
else if (userInput == terminate) {
return;
}
}
else if (userInput > answer) {
//Whitespace
System.out.println();
System.out.println("Too high!");
//Whitespace
System.out.println();
count = count + 1;
}
else if (userInput < answer) {
//Whitespace
System.out.println();
System.out.println("Too Low!");
//Whitespace
System.out.println();
count = count + 1;
}
//If guess is equal to random number, user wins and chooses whether or not to restart.
else if (userInput == answer) {
System.out.println("Great Job!");
System.out.println("To try again type 1, to quit type 0");
userInput = keyboard.nextInt();
if (userInput == restart) {
count = 0; //Resets count
answer = num.nextInt(range) + 1; //Resets random number
System.out.println("Enter the range of numbers you would like to guess from. EX: 5, 10, 100, 1000");
//Whitespace
System.out.println();
range = keyboard.nextInt();
//Whitespace
System.out.println();
System.out.println("Enter the amount of attempts you would like for this challenge");
//Whitespace
System.out.println();
attempts = keyboard.nextInt() - 1;
//Whitespace
System.out.println();
System.out.println("Enter a number between 1 and " + range + "\t");
//Whitespace
System.out.println();
}
else if (userInput == terminate) {
return;
}
}
}
while (userInput != answer || count < attempts);
}
//Exception Handler
catch (Exception all){
System.out.println("Please enter a valid integer and try again.");
}
}
3 Answers 3
Don't Repeat Yourself
With the 3rd occurrence of a sequence like add screen space, prompt user, parse anint
, you should abstract that:/** Prompt for an int. */ static int promptInt(Scanner keyboard, String prompt) { System.out.println(); System.out.println(prompt); System.out.println(); while (!keyboard.hasNextInt()) { keyboard.next(); System.out.println("try again:"); } return keyboard.nextInt(); }
use try-with-resources:
try (Scanner keyboard = new Scanner(System.in)) { System.out.println("Welcome To Elusive Numbers!"); range = promptInt(keyboard, "Enter the range of numbers " + "you would like to guess from. EX: 5, 10, 100, 1000"); attempts = promptInt(keyboard, "Enter the amount of attempts " + "you would like for this challenge") - 1; answer = promptInt(keyboard, "Enter number to be guessed " + "between 1 and " + range) + 1;
- Don't Repeat Yourself
the code repeats afterIf the user Chooses to restart
I got tired of scrolling through all those lines after that - just one more thing:
in the very end, you
- catch all
Exception
s: dangerous - tell the user
"Please enter a valid integer and try again."
, but terminate execution -
such behaviour may raise scorns
-
\$\begingroup\$ Thanks for taking the time to look at this and give some great advice! \$\endgroup\$RobotMan– RobotMan2018年02月11日 16:42:49 +00:00Commented Feb 11, 2018 at 16:42
-
\$\begingroup\$ I like the repeating of 'Don't Repeat Yourself', well played :) \$\endgroup\$Rob Audenaerde– Rob Audenaerde2018年02月13日 07:55:56 +00:00Commented Feb 13, 2018 at 7:55
You are in a good place. Some advice to make your code better:
Start using an IDE
The best one for Java developers, in my opinion, is IDEA IntelliJ. There is a community version, at the beginning of you coding adventure it'll be enough. I copied your code to my IDE and before I started reading it I get a message of potential duplicate code. It helps with extracting new methods and new classes, all that stuff. A proper IDE will do a lot of boring stuff for you.
Do not use comments
Your code should be self-explanatory, especially at that small part of code. You can avoid commenting creating a proper class and methods structure.
In your code, I created a new method:
private static void addWhitespace() { System.out.println(); }
Small change but thanks to that I replaced all 33 of
System.out.println()
, some which was with the comment//Whitespace
. We save some space and code is more readable.Do not write complicated and long methods
Your program has over 200 lines of code and there are no methods. It is hard to read. You can divide it into some parts, such as lines about starting messages to a user. You can extract it to separate method. Also, if you write some nested logic like
for inside for inside if
etc., divide it to different methods.
Ok, for the moment it should be enough. Please show next version of your code :)
-
\$\begingroup\$ Thank you for the great advice. I see what your saying about putting my repetitive code into methods so I don't take up as much space. In my Computer Science class my instructor is really passionate about documentation in the code so that people that do not code can understand it. I do have to admit I have gone overboard with the comments though. Ill post an updated version here as soon as I get the chance. \$\endgroup\$RobotMan– RobotMan2018年02月12日 17:49:37 +00:00Commented Feb 12, 2018 at 17:49
-
1\$\begingroup\$ I disagree with 2. The code will show HOW something works, but not WHY it is chosen to be implemented this way. Comments are meant for people reading the code and understanding why it is coded up in this fashion. For example, a comment could be 'I choose to use an ArrayList because of the o(1) get-performance'. I agree with leaving out comments that describe how the code works if that is trivial. \$\endgroup\$Rob Audenaerde– Rob Audenaerde2018年02月13日 07:55:26 +00:00Commented Feb 13, 2018 at 7:55
-
\$\begingroup\$ Right, I get what your saying. That's what i'm finding difficult in college, they are teaching us how to use logic to solve problems but not really how to write good code. My first example of this code worked, but there was a completely better way to write it. \$\endgroup\$RobotMan– RobotMan2018年02月13日 15:59:32 +00:00Commented Feb 13, 2018 at 15:59
what i would really recommend would be to split your code into sepreate code blocks... that increases the readability and reduces complexity...
do {
...
if (count >= attempts && userInput != answer) {
//If user guess is too low, output Too Low!
if (userInput < answer) {
printTooLow();
}
//If user guess is too high, output Too High!
else if (userInput > answer) {
printTooHigh();
}
printGameOver();
userInput = keyboard.nextInt();
//If the user Chooses to restart the game will reset, Else the program will exit
if (userInput == restart) {
reStart();
}
...
}
...
}while (userInput != answer || count < attempts);
that allows you to re-use code. you can use printTooHigh()
later again...
and that really seperates the code into relevant blocks (in your case: methods)
NOTE: the code above is just an example how you could adjust your code.
//Assign value to variables
is not a very good comment. (the//Whitespace
is pretty useless too) \$\endgroup\$