patternjavaMinor
Printing blocks in guessing game
Viewed 0 times
gameblocksprintingguessing
Problem
I wrote a guessing game and I want it to be reduced because there are a lot of
```
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 = 0 && 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 = 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 = 0 && seconds > 25 && seconds = 0 && seconds > 60 && seconds = 0
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 = 0 && 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 = 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 = 0 && seconds > 25 && seconds = 0 && seconds > 60 && seconds = 0
Solution
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.
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.
}Code Snippets
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.
}Context
StackExchange Code Review Q#146540, answer score: 4
Revisions (0)
No revisions yet.