I wrote a guessing game and I want it to be reduced because there are a lot of if
statements basically handling the same task with a minor difference. Is there an efficient way to handle all these tasks with less coding?
public class master {
public static void main(String[] args) {
int turn = 9;
System.out.println("Welcome to the guessing game");
System.out.println("You have 10 turns to guess");
System.out.println("Your time will be recorded,the faster you finish the better the points");
System.out.println("Enter your minimum value");
int min = In.getInt();
System.out.println("Enter your maximum value");
int max = In.getInt();
System.out.println("Press 1 to begin the game");
int begin = In.getInt();
if (max > min && begin == 1) {
int random = (int) ((max - min) * Math.random()) + min;
long lStartTime = System.nanoTime();
System.out.println("Enter your guess");
int guess = In.getInt();
while (guess != random && turn > 0) {
if (guess > random) {
System.out.println("Too High, try decreasing it");
} else if (guess < random) {
System.out.println("Too low, try increasing it");
}
guess = In.getInt();
turn--;
}
long lEndTime = System.nanoTime();
long difference = lEndTime - lStartTime;
long milliseconds = difference / 1000000;
long seconds = milliseconds / 1000;
if (turn <= 0) {
System.out.println("Computer wins, play again");
System.out.println("The final score is 0");
}
if (guess == random && turn >= 0 && seconds <= 10 && seconds >= 5) {
System.out.println("You won the game");
System.out.println("You have finished in under 10 seconds");
System.out.println("The final score is 500");
System.out.println("Turns Taken:" + " " + ((9 - turn) + 1));
System.out.println("Time Taken:" + " " + seconds + " " + "seconds");
}
if (guess == random && turn >= 0 && seconds < 5 && seconds >= 0) {
System.out.println("You won the game");
System.out.println("You have finished in under 5 seconds");
System.out.println("The final score is 1000");
System.out.println("Turns Taken:" + " " + ((9 - turn) + 1));
System.out.println("Time Taken:" + " " + seconds + " " + "seconds");
}
if (guess == random && turn >= 0 && seconds > 10 && seconds <= 25) {
System.out.println("You won the game");
System.out.println("You have finished in under 25 seconds");
System.out.println("The final score is 250");
System.out.println("Turns Taken:" + " " + ((9 - turn) + 1));
System.out.println("Time Taken:" + " " + seconds + " " + "seconds");
}
if (guess == random && turn >= 0 && seconds > 25 && seconds <= 60) {
System.out.println("You won the game");
System.out.println("You have finished in under 50 seconds");
System.out.println("The final score is 200");
System.out.println("Turns Taken:" + " " + ((9 - turn) + 1));
System.out.println("Time Taken:" + " " + seconds + " " + "seconds");
}
if (guess == random && turn >= 0 && seconds > 60 && seconds <= 180) {
System.out.println("You won the game");
System.out.println("You have finished in under 180 seconds, quite bad");
System.out.println("The final score is 150");
System.out.println("Turns Taken:" + " " + ((9 - turn) + 1));
System.out.println("Time Taken:" + " " + seconds + " " + "seconds");
}
if (guess == random && turn >= 0 && seconds > 180 && seconds <= 300) {
System.out.println("You won the game");
System.out.println("You have finished in under 180 seconds, very bad");
System.out.println("The final score is 100");
System.out.println("Turns Taken:" + " " + ((9 - turn) + 1));
System.out.println("Time Taken:" + " " + seconds + " " + "seconds");
}
if (guess == random && turn >= 0 && seconds > 300) {
System.out.println("You won the game");
System.out.println(
"You have finished in under 180 seconds, with that score it will be better if you try again");
System.out.println("The final score is 50");
System.out.println("Turns Taken:" + " " + ((9 - turn) + 1));
System.out.println("Time Taken:" + " " + seconds + " " + "seconds");
}
}
if (max < min) {
System.out.println("Make sure maximun is greater than minimum");
}
if (begin != 1) {
System.out.println("You did not begin the game");
}
}
}
2 Answers 2
//arrays of the scores and bounds
int[] scores = {1000, 500, 250, 200, 150, 100, 50, Integer.MAX_VALUE};
int[] secondOptions = {0, 5, 10, 25, 60, 180, 300, Integer.MAX_VALUE};
//decides if the text should be under or over
String underOrOver = (seconds < secondOptions[secondOptions.length - 2]) ? "under " : "over ";
//goes through each of the 7 iterations
for(int i = 0; i < 7; i++){
if((guess == random) && (turn >= 0) &&
((seconds >= secondOptions[i]) && (seconds < secondOptions[i + 1]))){
System.out.println("You won the game");
//seection in
System.out.println("You finished in " + underOrOver +
((seconds < 300) ? secondOptions[i + 1] : secondOptions[i]) + " seconds.");
System.out.println("Your final score is " + scores[i]);
System.out.println("Turns taken: " + ((9 - turn) + 1));
System.out.println("Time taken: " + seconds + " seconds.");
}
}
So it stores the two items you're checking against that change in arrays, loops through every spot of the array and checks which one it falls in, then displays the appropriate text.
Down to one for statement and one if statement.
-
\$\begingroup\$ Welcome to Code Review! The
guess == random
check could be moved around the loop instead of checking it over and over; perhaps also abreak
would be nice once one of the cases was actually true. Could you also elaborate on the last sentence - it doesn't seem very clear what's meant by that. Otherwise looks good, enjoy your stay! \$\endgroup\$ferada– ferada2016年11月09日 19:32:11 +00:00Commented Nov 9, 2016 at 19:32 -
1\$\begingroup\$ @ferada The last sentence was meant to be "Down to one for statement and one if statement.", not "Done to one ... if statement.". \$\endgroup\$Tyler McCarthy– Tyler McCarthy2016年11月09日 19:50:49 +00:00Commented Nov 9, 2016 at 19:50
If you put the seconds parameter in either increasing or decreasing order, then you only have to test one of the numbers.
Also, the tests can be nested. It makes sense to split out the first two tests since they are repeated several times.
if (guess == random && turn >= 0)
{
String underSeconds = "";
int score = 0;
System.out.println("You won the game.");
System.out.print("You have finished in under ");
if (seconds < 5)
{
underSeconds = "5 ";
score = 1000;
}
else if (seconds < 10)
{
underSeconds = "10 ";
score = 500;
}
else if (seconds < 25)
{
et cetera
}
// finish building and printing messages, using the variables
// underSeconds and score.
}