写一个小游戏的代码300行左右Java
时间: 2023-09-14 08:09:10 浏览: 46
以下是一个简单的Java文字游戏,代码约为300行:
```java
import java.util.Scanner;
public class TextAdventureGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name;
int age;
System.out.println("Welcome to the Text Adventure Game!");
System.out.print("Please enter your name: ");
name = sc.nextLine();
System.out.print("Please enter your age: ");
age = sc.nextInt();
sc.nextLine(); // consume the remaining newline character
System.out.println("Hello " + name + "! You are " + age + " years old.");
System.out.println("You wake up in a dark room with no memory of how you got there.");
System.out.println("You see two doors, one on your left and one on your right.");
System.out.print("Which door do you choose? (left/right): ");
String doorChoice = sc.nextLine();
if (doorChoice.equalsIgnoreCase("left")) {
System.out.println("You open the door on the left and see a long hallway.");
System.out.println("Do you want to go down the hallway? (yes/no): ");
String hallwayChoice = sc.nextLine();
if (hallwayChoice.equalsIgnoreCase("yes")) {
System.out.println("You walk down the hallway and see a treasure chest at the end.");
System.out.println("You open the chest and find a map that leads to hidden treasure!");
System.out.println("Congratulations! You win the game!");
} else {
System.out.println("You decide not to go down the hallway and go back to the room.");
System.out.println("You see the other door and decide to try that one instead.");
System.out.println("You open the door on the right and see a staircase going up.");
System.out.println("Do you want to go up the stairs? (yes/no): ");
String stairsChoice = sc.nextLine();
if (stairsChoice.equalsIgnoreCase("yes")) {
System.out.println("You climb up the stairs and find yourself on the roof of a castle.");
System.out.println("You see a rope hanging down from the roof.");
System.out.println("Do you want to climb down the rope? (yes/no): ");
String ropeChoice = sc.nextLine();
if (ropeChoice.equalsIgnoreCase("yes")) {
System.out.println("You climb down the rope and escape from the castle!");
System.out.println("Congratulations! You win the game!");
} else {
System.out.println("You decide not to climb down the rope and go back to the room.");
System.out.println("You see the other door and decide to try that one instead.");
System.out.println("You open the door on the left and see a long hallway.");
System.out.println("You walk down the hallway and suddenly a trapdoor opens below you!");
System.out.println("You fall into a pit and die. Game over!");
}
} else {
System.out.println("You decide not to go up the stairs and go back to the room.");
System.out.println("You see the other door and decide to try that one instead.");
System.out.println("You open the door on the left and see a long hallway.");
System.out.println("You walk down the hallway and suddenly a trapdoor opens below you!");
System.out.println("You fall into a pit and die. Game over!");
}
}
} else {
System.out.println("You open the door on the right and see a staircase going up.");
System.out.println("Do you want to go up the stairs? (yes/no): ");
String stairsChoice = sc.nextLine();
if (stairsChoice.equalsIgnoreCase("yes")) {
System.out.println("You climb up the stairs and find yourself on the roof of a castle.");
System.out.println("You see a rope hanging down from the roof.");
System.out.println("Do you want to climb down the rope? (yes/no): ");
String ropeChoice = sc.nextLine();
if (ropeChoice.equalsIgnoreCase("yes")) {
System.out.println("You climb down the rope and escape from the castle!");
System.out.println("Congratulations! You win the game!");
} else {
System.out.println("You decide not to climb down the rope and go back to the room.");
System.out.println("You see the other door and decide to try that one instead.");
System.out.println("You open the door on the left and see a long hallway.");
System.out.println("You walk down the hallway and suddenly a trapdoor opens below you!");
System.out.println("You fall into a pit and die. Game over!");
}
} else {
System.out.println("You decide not to go up the stairs and go back to the room.");
System.out.println("You see the other door and decide to try that one instead.");
System.out.println("You open the door on the left and see a long hallway.");
System.out.println("You walk down the hallway and suddenly a trapdoor opens below you!");
System.out.println("You fall into a pit and die. Game over!");
}
}
}
}
```