How can I make this code shorter? The code I am using is so long. Just making one simple level is taking about 100 lines of code.
public class Sister_age_guesser {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Sherlock holmes - The game");
System.out.println(" This is a mystery game where you have to find clues after solving math sums");
System.out.println("Write your name ");
String name = input.nextLine();
System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("Instructions");
System.out.println("1.This game has 1 level ");
System.out.println("2.Clues will be given before you write down the answer.Writing the correct answer will unlock the next clue ");
System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ");
System.out.println(" FLORENTINES MURDER ");
System.out.println(" ");
int SisterBirthday = 1997;
System.out.println("FIND THE MURDER WEAPON");
System.out.println(" ");
System.out.println("Clues :-");
System.out.println(" ");
System.out.println(" 1. This number is 1000 added to itself. 2. cut 6 into 2 pieces and take half of it and subtract it from the result from the previous clue ");
int number = input.nextInt();
if(number < SisterBirthday)
{
System.out.println(name + " , you need more practice" + name);
System.exit(0);
}
else if(number > SisterBirthday)
{
System.out.println(" cant u solve this simple clue " + name + " ?");
System.exit(0);
}
else
System.out.println("Good thing you found out the murder weapon.We found some fingerprints on the weapon . Keep it up " + name + " !");
System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ");
System.out.println("TRACE THE CRIMINAL WITH THE FINGERPRINTS");
System.out.println("Clues:-");
System.out.println("This added with itself gives nothing.Neither a positive nor a negative");
int clue2 = 0;
int number2 = input.nextInt();
if(number2 < clue2)
{
System.out.println("You need to get your eyes checked !!" + name +" Can't you just match the fingerprint found on the weapon with the fingerprints we have got.We have got many other capabale officers who can do this job" + name + "!!!!" );
System.exit(0);
}
else if (number2 > clue2)
{
System.out.println("wonder why the case was assigned to you when you are not even capable of tracing a fingerprint" + name);
System.exit(0);
}
else
System.out.println(" Ah ! very Good !" + name + " .This is the type of work I expect from you!" );
System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("CAPTURE THE CRIMINAL");
System.out.println("Clues:-");
System.out.println("Subtract 1999 from 2000 and you get your answer");
int clue3 = 1;
int number3 = input.nextInt();
if(number3 < clue3)
{
System.out.println("Oh no"+ name + "!! The criminal escaped !!! ");
System.exit(0);
}
else if (number3 > clue3)
{
System.out.println("Oh no"+ name + "!! The criminal escaped !!! ");
System.exit(0);
}
else
System.out.println(" Oh yes ! We managed to catch the criminal and all the credit goes to you " + name + " !");
System.out.println(" Level 2");
}
}
StackOverflow
](stackoverflow.com)), I would use a system where each question returnstrue
orfalse
, and you run the codeif (question1() && question2() && etc...) you_completed_ the_game() else you_lost_the_game
. This works because Java will run the first question and, if it returns true, will try the next one. If it returns false...false && anything
is false, so it doesn't run the next thing (what's the point?). Using this prevents a Stack Overflow. Please, somebody, fix this and make it an answer. \$\endgroup\$if (question1() && question2() ...)
is not recursion. It is short-circuit evaluation of conditionals. \$\endgroup\$