我正在为我的编程制作一个基于文本的 Rpg Class 并且遇到了错误
I Am Making A Text Based Rpg For My Programing Class And Have Encountered Errors
在我的代码中,我使用 x 和 y 线在游戏中四处移动,它应该可以正常工作,但在大约 490 行时,它给了我一个 else 没有,即使有一个,当我尝试使用 wasd它去了它应该去的地方
(抱歉代码转储,我无法确定我是新手的问题 java)
/**
* @(#)FSE.java
*
* FSE application
*
* @author
* @version 1.00 2015/1/7
*/
import java.io.*;
import java.util.Random;
public class FSE {
public static void main(String[] args) {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
// Declaring Variables
String Name, Move, Attack;
int x, y, Combat, Creature, HP, HP2, Strike, Strike2, loop;
x=0;
y=0;
Combat=0;
Creature=0;
Move="";
Attack="";
HP=0;
HP2=0;
Strike=0;
Strike2=0;
loop=0;
// Introduction to start the game off
System.out.println("Your Awake Traveler Do You Remember What Happened");
try {
Thread.sleep(2500L);
}
catch (Exception e) {}
System.out.println("You Look Around At What looks Like A Old Cave And See A Colapsed Entrance To The North");
try {
Thread.sleep(2500L);
}
catch (Exception e) {}
System.out.println("Do Remember What Your Name Is");
try{
Name=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
while(loop==0){
System.out.println("What Way Would You Like To Navigate Use WASD To Choose A Direction");
try{
Move=input.readLine();
}
// Movement Control Keys
catch (IOException ioe){
System.out.println("input error");
}
if(Move.equals("w")){ // Upward Movement
y+=1;
System.out.println("*****"+y);
}
else if(Move.equals("d")){ // Right Movement
x+=1;
}
else if(Move.equals("s")){ // Downward Movement
y-=1;
}
else{
x-=1; // Left Movement Key ("a")
}
if (y==-1 && x==0){ // First-Entrance into the game's combat
System.out.println("You have now entered the cave.");
Combat=1;
while(Combat==1);{
System.out.println("A Goblin Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-2 && x==0){ // Path leading downwards from entrance pointt
System.out.println("You have entered a new room in the cave!");
Combat=1;
while(Combat==1);{
System.out.println("A Imp Has Attacked");
HP=20;
HP2=7;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Imp Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(2);
Strike2+=2;
HP-=Strike2;
System.out.println("The Imp Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Imp And May Continue In Your Quest");
}
}
}
}
else if (y==-3 && x==0){
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Goblin Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-4 && x==0){
System.out.println("You have entered the final room on the path! The path does not go any further.");
Combat=1;
while(Combat==1);{
System.out.println("A Troll Has Attacked");
HP=20;
HP2=10;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Troll Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(7);
Strike2+=7;
HP-=Strike2;
System.out.println("The Troll Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Troll And May Continue In Your Quest");
}
}
}
}
else if (y==-1 && x==1){ // Right path from the entrance point
System.out.println("You have entered a new room in the cave!");
Combat=1;
while(Combat==1);{
System.out.println("A Goblin Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-1 && x==2){
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Giant Spider Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Giant Spider Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(6);
Strike2+=6;
HP-=Strike2;
System.out.println("The Giant Spider Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Giant Spider And May Continue In Your Quest");
}
}
}
}
else if (y==-1 && x==3){
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Imp Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Imp Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(2);
Strike2+=2;
HP-=Strike2;
System.out.println("The Imp Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Imp And May Continue In Your Quest");
}
}
}
}
else if (y==-2 && x==3){ //Path going downwards towards the boss
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Giant Spider Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-3 && x==3){
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Goblin Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-4 && x==3){
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Goblin Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-4 && x==1){
System.out.println("You have entered the final room in the cave! You are now in the Demon's Domain!"); //Final Boss Battle
System.out.println("*"+y+"*"+x+"*");
Combat=1;
while(Combat==1);{
System.out.println("The Demon has attacked");
HP=20;
HP2=15;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(8);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Demon! Conagradulations!");
}
}
}
}
}
else if (y==-4 && x==2){
System.out.println("You have entered the final room in the cave! You are now in the Demon's Domain!"); //Final Boss Battle
system.out.println("*"+y+"*"+x+"*");
Combat=1;
while(Combat==1);{
System.out.println("The Demon has attacked");
HP=20;
HP2=15;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(8);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Demon! Conagradulations!");
}
}
}
}
}
// Spaces in the game where you are not allowed to go
if (y==1 && x==0);{
System.out.println("You cannot go this way.");
y=0;
x=0;
}
else if (y==0 && x==-1){
System.out.println("You cannot go this way.");
y=0;
x=0;
}
else if (y==0 && x==1){
System.out.println("You cannot go this way.");
y=-1;
x=0;
}
else if (y==0 && x==2){
System.out.println("You cannot go this way.");
y=-1;
x=2;
}
else if (y==0 && x==3){
System.out.println("You cannot go this way.");
y=-1;
x=3;
}
else if (y==-1 && x==4){
System.out.println("You cannot go this way.");
y=-1;
x=3;
}
else if (y==-2 && x==4){
System.out.println("You cannot go this way.");
y=-2;
x=3;
}
else if (y==-3 && x==4){
System.out.println("You cannot go this way.");
y=-3;
x=3;
}
else if (y==-4 && x==4){
System.out.println("You cannot go this way.");
y=-4;
x=3;
}
else if (y==-5 && x==0){
System.out.println("You cannot go this way.");
y=-4;
x=0;
}
else if (y==-2 && x==1){
System.out.println("You cannot go this way.");
y=-1;
x=0;
}
else if (y==-2 && x==-2){
System.out.println("You cannot go this way.");
y=-1;
x=3;
}
else if (y==-3 && x==1){
System.out.println("You cannot go this way.");
y=-3;
x=0;
}
else if (y==-5 && x==3){
System.out.println("You cannot go this way.");
y=-4;
x=3;
}
else{
System.out.println("You cannot go this way")
y=-3;
x=3;
}
}
}
我尝试了 3 次从 Stack Overflow 复制和粘贴您的代码。
您有 4 个语法错误。您粘贴的代码甚至无法编译。
你最大的问题是你的整个 Java 应用程序只有一个方法(主要)。
Java 的设计目的是让您可以将代码分解成很多很多方法,如果有必要,还可以分解成很多 classes。当你创建这样的东西时,你应该使用 model / view / controller (MVC) pattern。 MVC 模式允许您分离您的关注点,并让您能够一次专注于程序的一小部分。
先看看你的介绍吧。您两次显示消息,然后等待。下面介绍如何创建一个方法来执行此操作。
private static void displayMessageAndWait(String message) {
System.out.println(message);
try {
Thread.sleep(2500L);
} catch (InterruptedException e) {
}
}
用这个方法,你的介绍现在是这样的。
// Introduction to start the game off
String message = "You're Awake Traveler. Do You Remember What Happened?";
displayMessageAndWait(message);
message = "You Look Around At What looks Like A Old Cave " +
"And See A Collapsed Entrance To The North.";
displayMessageAndWait(message);
您是否看到创建方法如何记录代码的作用?通过将您的代码分解成非常小的片段,并将这些片段放入一个方法中,您可以让您的 Java 应用程序更容易理解,也更容易让其他人理解。
接下来,我们将看一下取名部分。我们将把代码放在一个名为 getName 的方法中。聪明啊
private static String getName(BufferedReader input) {
System.out.println("Do You Remember What Your Name Is?");
try {
String name = input.readLine();
return name;
} catch (IOException ioe) {
ioe.printStackTrace();
return "";
}
}
现在,main 方法中的代码如下所示。
name = getName(input);
Java 变量名和方法名以小写字母开头。这样我们就可以一眼区分 class 名称和方法名称或变量名称。
到目前为止,我所做的只是分解您的代码。 Java 的真正威力在于当您创建 class 可用于为您的 Java 应用程序建模时。
这里有一个房间模型 class,作为示例。
public class Room {
private boolean isCreatureInRoom;
private int x;
private int y;
private String description;
public Room(int x, int y, String description) {
this.x = x;
this.y = y;
this.description = description;
this.isCreatureInRoom = false;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public String getDescription() {
return description;
}
public boolean isCreatureInRoom() {
return isCreatureInRoom;
}
public void setCreatureInRoom(boolean isCreatureInRoom) {
this.isCreatureInRoom = isCreatureInRoom;
}
}
现在,我们可以通过为我们想要的每个房间创建 Room class 的实例来创建任意数量的房间。
希望这些信息足以让您返回并将您的程序分解为许多方法和 classes。
在我的代码中,我使用 x 和 y 线在游戏中四处移动,它应该可以正常工作,但在大约 490 行时,它给了我一个 else 没有,即使有一个,当我尝试使用 wasd它去了它应该去的地方 (抱歉代码转储,我无法确定我是新手的问题 java)
/**
* @(#)FSE.java
*
* FSE application
*
* @author
* @version 1.00 2015/1/7
*/
import java.io.*;
import java.util.Random;
public class FSE {
public static void main(String[] args) {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
// Declaring Variables
String Name, Move, Attack;
int x, y, Combat, Creature, HP, HP2, Strike, Strike2, loop;
x=0;
y=0;
Combat=0;
Creature=0;
Move="";
Attack="";
HP=0;
HP2=0;
Strike=0;
Strike2=0;
loop=0;
// Introduction to start the game off
System.out.println("Your Awake Traveler Do You Remember What Happened");
try {
Thread.sleep(2500L);
}
catch (Exception e) {}
System.out.println("You Look Around At What looks Like A Old Cave And See A Colapsed Entrance To The North");
try {
Thread.sleep(2500L);
}
catch (Exception e) {}
System.out.println("Do Remember What Your Name Is");
try{
Name=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
while(loop==0){
System.out.println("What Way Would You Like To Navigate Use WASD To Choose A Direction");
try{
Move=input.readLine();
}
// Movement Control Keys
catch (IOException ioe){
System.out.println("input error");
}
if(Move.equals("w")){ // Upward Movement
y+=1;
System.out.println("*****"+y);
}
else if(Move.equals("d")){ // Right Movement
x+=1;
}
else if(Move.equals("s")){ // Downward Movement
y-=1;
}
else{
x-=1; // Left Movement Key ("a")
}
if (y==-1 && x==0){ // First-Entrance into the game's combat
System.out.println("You have now entered the cave.");
Combat=1;
while(Combat==1);{
System.out.println("A Goblin Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-2 && x==0){ // Path leading downwards from entrance pointt
System.out.println("You have entered a new room in the cave!");
Combat=1;
while(Combat==1);{
System.out.println("A Imp Has Attacked");
HP=20;
HP2=7;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Imp Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(2);
Strike2+=2;
HP-=Strike2;
System.out.println("The Imp Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Imp And May Continue In Your Quest");
}
}
}
}
else if (y==-3 && x==0){
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Goblin Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-4 && x==0){
System.out.println("You have entered the final room on the path! The path does not go any further.");
Combat=1;
while(Combat==1);{
System.out.println("A Troll Has Attacked");
HP=20;
HP2=10;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Troll Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(7);
Strike2+=7;
HP-=Strike2;
System.out.println("The Troll Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Troll And May Continue In Your Quest");
}
}
}
}
else if (y==-1 && x==1){ // Right path from the entrance point
System.out.println("You have entered a new room in the cave!");
Combat=1;
while(Combat==1);{
System.out.println("A Goblin Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-1 && x==2){
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Giant Spider Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Giant Spider Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(6);
Strike2+=6;
HP-=Strike2;
System.out.println("The Giant Spider Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Giant Spider And May Continue In Your Quest");
}
}
}
}
else if (y==-1 && x==3){
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Imp Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Imp Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(2);
Strike2+=2;
HP-=Strike2;
System.out.println("The Imp Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Imp And May Continue In Your Quest");
}
}
}
}
else if (y==-2 && x==3){ //Path going downwards towards the boss
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Giant Spider Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-3 && x==3){
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Goblin Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-4 && x==3){
System.out.println("You have entered the next room.");
Combat=1;
while(Combat==1);{
System.out.println("A Goblin Has Attacked");
HP=20;
HP2=8;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(5);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");
}
}
}
}
else if (y==-4 && x==1){
System.out.println("You have entered the final room in the cave! You are now in the Demon's Domain!"); //Final Boss Battle
System.out.println("*"+y+"*"+x+"*");
Combat=1;
while(Combat==1);{
System.out.println("The Demon has attacked");
HP=20;
HP2=15;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(8);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Demon! Conagradulations!");
}
}
}
}
}
else if (y==-4 && x==2){
System.out.println("You have entered the final room in the cave! You are now in the Demon's Domain!"); //Final Boss Battle
system.out.println("*"+y+"*"+x+"*");
Combat=1;
while(Combat==1);{
System.out.println("The Demon has attacked");
HP=20;
HP2=15;
System.out.println("Press A To Attack");
try{
Attack=input.readLine();
}
catch (IOException ioe){
System.out.println("input error");
}
if(Attack.equals("A")){
Random randomGenerator=new Random();
Strike=randomGenerator.nextInt(10);
Strike+=5;
HP2-=Strike;
System.out.println("You Did "+Strike+" Damage");
System.out.println("The Goblin Has "+HP2+" Life Left");
Strike2=randomGenerator.nextInt(8);
Strike2+=5;
HP-=Strike2;
System.out.println("The Goblin Did "+Strike2+" Damage");
System.out.println("You Have "+HP+" Life Left");
if(HP<=0){
System.out.println("You Died. Game Over.");
System.exit(0);
}
else if(HP2<=0){
Combat=0;
System.out.println("You Have Defeated The Demon! Conagradulations!");
}
}
}
}
}
// Spaces in the game where you are not allowed to go
if (y==1 && x==0);{
System.out.println("You cannot go this way.");
y=0;
x=0;
}
else if (y==0 && x==-1){
System.out.println("You cannot go this way.");
y=0;
x=0;
}
else if (y==0 && x==1){
System.out.println("You cannot go this way.");
y=-1;
x=0;
}
else if (y==0 && x==2){
System.out.println("You cannot go this way.");
y=-1;
x=2;
}
else if (y==0 && x==3){
System.out.println("You cannot go this way.");
y=-1;
x=3;
}
else if (y==-1 && x==4){
System.out.println("You cannot go this way.");
y=-1;
x=3;
}
else if (y==-2 && x==4){
System.out.println("You cannot go this way.");
y=-2;
x=3;
}
else if (y==-3 && x==4){
System.out.println("You cannot go this way.");
y=-3;
x=3;
}
else if (y==-4 && x==4){
System.out.println("You cannot go this way.");
y=-4;
x=3;
}
else if (y==-5 && x==0){
System.out.println("You cannot go this way.");
y=-4;
x=0;
}
else if (y==-2 && x==1){
System.out.println("You cannot go this way.");
y=-1;
x=0;
}
else if (y==-2 && x==-2){
System.out.println("You cannot go this way.");
y=-1;
x=3;
}
else if (y==-3 && x==1){
System.out.println("You cannot go this way.");
y=-3;
x=0;
}
else if (y==-5 && x==3){
System.out.println("You cannot go this way.");
y=-4;
x=3;
}
else{
System.out.println("You cannot go this way")
y=-3;
x=3;
}
}
}
我尝试了 3 次从 Stack Overflow 复制和粘贴您的代码。
您有 4 个语法错误。您粘贴的代码甚至无法编译。
你最大的问题是你的整个 Java 应用程序只有一个方法(主要)。
Java 的设计目的是让您可以将代码分解成很多很多方法,如果有必要,还可以分解成很多 classes。当你创建这样的东西时,你应该使用 model / view / controller (MVC) pattern。 MVC 模式允许您分离您的关注点,并让您能够一次专注于程序的一小部分。
先看看你的介绍吧。您两次显示消息,然后等待。下面介绍如何创建一个方法来执行此操作。
private static void displayMessageAndWait(String message) {
System.out.println(message);
try {
Thread.sleep(2500L);
} catch (InterruptedException e) {
}
}
用这个方法,你的介绍现在是这样的。
// Introduction to start the game off
String message = "You're Awake Traveler. Do You Remember What Happened?";
displayMessageAndWait(message);
message = "You Look Around At What looks Like A Old Cave " +
"And See A Collapsed Entrance To The North.";
displayMessageAndWait(message);
您是否看到创建方法如何记录代码的作用?通过将您的代码分解成非常小的片段,并将这些片段放入一个方法中,您可以让您的 Java 应用程序更容易理解,也更容易让其他人理解。
接下来,我们将看一下取名部分。我们将把代码放在一个名为 getName 的方法中。聪明啊
private static String getName(BufferedReader input) {
System.out.println("Do You Remember What Your Name Is?");
try {
String name = input.readLine();
return name;
} catch (IOException ioe) {
ioe.printStackTrace();
return "";
}
}
现在,main 方法中的代码如下所示。
name = getName(input);
Java 变量名和方法名以小写字母开头。这样我们就可以一眼区分 class 名称和方法名称或变量名称。
到目前为止,我所做的只是分解您的代码。 Java 的真正威力在于当您创建 class 可用于为您的 Java 应用程序建模时。
这里有一个房间模型 class,作为示例。
public class Room {
private boolean isCreatureInRoom;
private int x;
private int y;
private String description;
public Room(int x, int y, String description) {
this.x = x;
this.y = y;
this.description = description;
this.isCreatureInRoom = false;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public String getDescription() {
return description;
}
public boolean isCreatureInRoom() {
return isCreatureInRoom;
}
public void setCreatureInRoom(boolean isCreatureInRoom) {
this.isCreatureInRoom = isCreatureInRoom;
}
}
现在,我们可以通过为我们想要的每个房间创建 Room class 的实例来创建任意数量的房间。
希望这些信息足以让您返回并将您的程序分解为许多方法和 classes。