生成个java文字游戏代码
时间: 2024-05-13 14:13:44 浏览: 15
Sure! Here is a simple Java code for a text-based game:
```java
import java.util.Scanner;
public class TextGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
System.out.println("Welcome to the Text Adventure Game!");
System.out.print("Enter your name: ");
name = input.nextLine();
System.out.println("\nHello " + name + "!");
System.out.println("\nYou find yourself in a dark cave. There are two paths ahead of you.");
System.out.println("Do you choose to go left or right?");
String path = input.nextLine();
if (path.equalsIgnoreCase("left")) {
System.out.println("You encounter a giant spider!");
System.out.println("Quick, choose your weapon: sword or bow?");
String weapon = input.nextLine();
if (weapon.equalsIgnoreCase("sword")) {
System.out.println("You strike the spider with your sword and defeat it!");
} else if (weapon.equalsIgnoreCase("bow")) {
System.out.println("You fire an arrow at the spider and it falls to the ground!");
} else {
System.out.println("You freeze in fear and the spider attacks you!");
System.out.println("GAME OVER");
}
} else if (path.equalsIgnoreCase("right")) {
System.out.println("You slip and fall into a pit filled with snakes!");
System.out.println("Quick, choose your escape plan: climb or swim?");
String plan = input.nextLine();
if (plan.equalsIgnoreCase("climb")) {
System.out.println("You climb out of the pit and continue on your journey.");
} else if (plan.equalsIgnoreCase("swim")) {
System.out.println("You swim through the snakes and emerge on the other side of the pit.");
System.out.println("You continue on your journey.");
} else {
System.out.println("The snakes catch up to you and you are bitten multiple times!");
System.out.println("GAME OVER");
}
} else {
System.out.println("You can't decide which path to take and are stuck in the cave forever!");
System.out.println("GAME OVER");
}
input.close();
}
}
```
Hope you have fun playing with it!