patternjavaModerate
Little text game in Java
Viewed 0 times
textgamelittlejava
Problem
I'm a beginner in the coding world, and I've been learning Java recently. I tried to make a small text game with the limited knowledge that I currently have of Java. I would like you to tell me ways to improve my code, so that I can learn it better. I currently feel my code is a bit messed up, but at least it's a start.
```
import java.util.*; public class GameMain {
public static void main(String []args) {
Scanner rep = new Scanner(System.in); //Scanner variable
String nm; //Stores the name of the player.
String d1,d2,d3; //Store the decisions of player.
int age; //Stores the age of player.
String line2, line3, line4; //Text for the lines.
//The next few lines contain text for the dialogues and lines.
line2 = "What in the.......!!! He shoots you in the head. GAMEOVER";
line3 = "Good. Lets GO! As you both proceed, suddenly a masked man enters the \ncell with a gun and holds the man at gunpoint. You start to shit your pants";
line4 = "You slowly raise your arms in surrender. As you are getting down on your knees, another man shouts from\n15"
+ " outside'HEY! VIKTOR! MAKE IT QUICK!'. The man gets distracted. You see this as an opportunity";
//Next few lines enforce Age Limit.
System.out.println("Please enter your age.");
age = rep.nextInt();
if (age>13) {
//This stores the name of the player to the variable nm.
System.out.println("Hello. Please Enter your Name :");
}else {
System.out.println("You are not old enough.");
System.exit(0);
}
nm=rep.next(); /allows the user to enter a name, if he/she is older than 13/
//Next lines print out the first dialogues and decision.
System.out.print("[note-type exit at any time to exit] Wake UP! Goddamnit!\n Looks like they overdosed you a bit. You are ");
System.out.print(nm);
System.
```
import java.util.*; public class GameMain {
public static void main(String []args) {
Scanner rep = new Scanner(System.in); //Scanner variable
String nm; //Stores the name of the player.
String d1,d2,d3; //Store the decisions of player.
int age; //Stores the age of player.
String line2, line3, line4; //Text for the lines.
//The next few lines contain text for the dialogues and lines.
line2 = "What in the.......!!! He shoots you in the head. GAMEOVER";
line3 = "Good. Lets GO! As you both proceed, suddenly a masked man enters the \ncell with a gun and holds the man at gunpoint. You start to shit your pants";
line4 = "You slowly raise your arms in surrender. As you are getting down on your knees, another man shouts from\n15"
+ " outside'HEY! VIKTOR! MAKE IT QUICK!'. The man gets distracted. You see this as an opportunity";
//Next few lines enforce Age Limit.
System.out.println("Please enter your age.");
age = rep.nextInt();
if (age>13) {
//This stores the name of the player to the variable nm.
System.out.println("Hello. Please Enter your Name :");
}else {
System.out.println("You are not old enough.");
System.exit(0);
}
nm=rep.next(); /allows the user to enter a name, if he/she is older than 13/
//Next lines print out the first dialogues and decision.
System.out.print("[note-type exit at any time to exit] Wake UP! Goddamnit!\n Looks like they overdosed you a bit. You are ");
System.out.print(nm);
System.
Solution
Welcome to Codereview. It's great that you're doing something to learn and trying to make it fun, keep it up!
Use meaningful variable names.
Use line breaks for readability
It looks like you're familiar with the new line character, in a lot of your code you're calling
For example your first line could be:
Follow Java Conventions
Use meaningful variable names.
d1,d2,d3 cost little to simply name decision1 or better yet punchDecision or followDecision same for nm to just name or playerName, it'll help you down the line if this becomes a big project and is a generally good thing to pick up as early as possible.Use line breaks for readability
It looks like you're familiar with the new line character, in a lot of your code you're calling
print multiple times which has a cost, and in some other places you have some seriously horizontally unkempt lines, it helps if you split that up into several lines for readability.For example your first line could be:
System.out.println(
"What do you do? "
+ "\nA. You try to take the gun from the man."
+ "\nB. You slowly raise your arms to display that you pose no danger."
);Follow Java Conventions
- Write variables on their own line.
- import statements should be at the top and separate from class declaration.
- Everything within curly brackets should be tabbed, including the main method.
Code Snippets
System.out.println(
"What do you do? "
+ "\nA. You try to take the gun from the man."
+ "\nB. You slowly raise your arms to display that you pose no danger."
);Context
StackExchange Code Review Q#104878, answer score: 11
Revisions (0)
No revisions yet.