一个小文字游戏

时间:2014-01-16 00:29:24

标签: java compiler-errors

我是一个完整的java新手,过去几天我在电脑课上闲暇时间,我一直在为一个小游戏而努力。

我的问题是,当我尝试编译它时,它给了我16个错误,我完全迷失了如何解决它,而且我想到的唯一一件事就是我的代码可能不正确订购。感谢任何帮助,这是我的代码:

/**RULES AND TIPS:
* IF YOU INPUT AN ANSWER THAT IS NOT ONE OF THE CHOICES YOU WILL DIE AND RESTART
* DO NOT LOOK AT THE CODE, IT KILLS THE FUN MY DUDE:/
* SOMETIMES THERE IS MORE THAN ONE CORRECT ANSWER =)
* WEAPONS AND ARMOR WILL AID IN FIGHTING SITUATIONS
********IN PROGRESS RIGHT NOW, WILL ADD THESE ALL SOME DAY********
* ITEM LIST:
* IRON DAGGER +1 STRENGTH
* IRON SWORD +2 STRENGTH
* IRON BROADSWORD +3 STRENGTH
* STEEL DAGGER +2 STRENGTH
* STEEL SWORD +3 STRENGTH
* STEEL BROADSWORD +4 STRENGTH
* MYTHRIL DAGGER +3 STRENGTH
* MYTHRIL SWORD +4 STRENGTH
* MYTHRIL BROADSWORD +5 STRENGTH
* 
* CLOAK +1 DODGE
* MAGIC CLOAK +3 DODGE
*/

import java.util.Scanner;

public class Lame
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    int answer;

    System.out.println("Welcome to Rod's java game");
    System.out.println("Have fun!");
    System.out.println("*****RULES AND TIPS ARE IN THE FIRST AND ONLY COMMENT*****");
    System.out.println("You come to a fork in the road. Which way do you go?");
    System.out.println("Enter 1 for left or 2 for right");
    answer = input.nextInt();

    if (answer == 1)
    {
      Dead();
    }
    if (answer == 2)
    {
      Right();
    }
  }
  public static void Dead()
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Wrong decision, you died. Recompile and try a different path!");
  }
  public static void Right()
  {
    int answer1;

    Scanner input = new Scanner(System.in);
    System.out.println("");
    System.out.println("You walk down the right path and come to a peculiar house.");
    System.out.println("It's getting dark outside and the sign says beware of wolves.");
    System.out.println("Do you stay in the house for the night or sleep under a tree?");
    System.out.println("Enter 1 for the house or 2 for the tree");
    answer1 = input.nextInt();

    if (answer1 == 1)
    {
      House();
    }
    if (answer1 == 2)
    {
      Tree();
    }
  }
  public static void House()
  {
    int answer2;

    Scanner input = new Scanner(System.in);
    System.out.println("");
    System.out.println("You survived the night and continue your adventure!");
    System.out.println("");
    System.out.println("Beyond the house there is a man being robbed by a thug");
    System.out.println("What do you do?");
    System.out.println("Enter 1 to continue walking or 2 to help him");
    answer2 = input.nextInt();

    if (answer2 == 1)
    {
      Dead1();
    }
    if (answer2 == 2)
    {
      Help();
    }
  }
  public static void Tree()
  {
    int answer4;

    Scanner input = new Scanner(System.in);
    System.out.println("");
    System.out.println("You survived the night and continue your adventure!");
    System.out.println("");
    System.out.println("Beyond the house there is a man being robbed by a thug");
    System.out.println("What do you do?");
    System.out.println("Enter 1 to continue walking or 2 to help him");
    answer2 = input.nextInt();

    if (answer4 == 1)
    {
      Dead1();
    }
    if (answer4 == 2)
    {
      Help();
    }
    public static void Dead1()
    {
      Scanner input = new Scanner(System.in);
      System.out.println("As you attempt to continue walking, the thug stops you");
      System.out.println("Before you can say anything, he unsheaths his sword and kills               you!");
      System.out.println("Wrong decision, you died. Recompile and try a different path!");
    }
    public static void Help()
    {
      int answer3;

      Scanner input = new Scanner(System.in);
      System.out.println("");
      System.out.println("As you run over to help the man, the thug attacks you!");
      System.out.println("You defeat the thug easily, and he drops his Iron Sword!");
      System.out.println("****IRON SWORD OBTAINED****");
      System.out.println("");
      System.out.println("");
      System.out.println("You continue your journey.");
      System.out.println("After a few hours of walking, you get to a dark castle.");
      System.out.println("Your sense of adventure draws you towards the castle");
      System.out.println("You enter, and are immediately greeted by a spiral staircase.");
      System.out.println("Enter 1 to go up the staircase or 2 to continue walking");
      answer3 = input.nextInt();

      if (answer3 == 1)
      {
        Staircase();
      }
      if (answer3 == 2)
      {
        Continuewalking();
      }
     public static void Staircase()
     {
       Scanner input = new Scanner(System.in);
       System.out.println("You die. The end.");
     }
     public static void Continuewalking()
     {
       Scanner input = new Scanner(System.in);
       System.out.println("You die. The end.");
     }
    }
  }
}

感谢您提前获得帮助,我试图快速说出游戏,以便获得一些帮助:)

编辑:这些是我得到的错误:

  找到16个错误:文件:C:\ Users \ Rod \ Desktop \ java files \ Lame.java   [line:116]错误:非法启动表达式文件:   C:\ Users \ Rod \ Desktop \ java files \ Lame.java [line:116]错误:非法   表达式文件的开头:C:\ Users \ Rod \ Desktop \ java files \ Lame.java   [line:116]错误:';'预期的文件:C:\ Users \ Rod \ Desktop \ java   files \ Lame.java [line:116]错误:';'预期档案:   C:\ Users \ Rod \ Desktop \ java files \ Lame.java [line:123]错误:非法   表达式文件的开头:C:\ Users \ Rod \ Desktop \ java files \ Lame.java   [line:123]错误:非法启动表达式文件:   C:\ Users \ Rod \ Desktop \ java files \ Lame.java [line:123]错误:';'   预期文件:C:\ Users \ Rod \ Desktop \ java files \ Lame.java [line:123]   错误:';'预期文件:C:\ Users \ Rod \ Desktop \ java files \ Lame.java   [line:149]错误:非法启动表达式文件:   C:\ Users \ Rod \ Desktop \ java files \ Lame.java [line:149]错误:非法   表达式文件的开头:C:\ Users \ Rod \ Desktop \ java files \ Lame.java   [line:149]错误:';'预期的文件:C:\ Users \ Rod \ Desktop \ java   files \ Lame.java [line:149]错误:';'预期档案:   C:\ Users \ Rod \ Desktop \ java files \ Lame.java [line:154]错误:非法   表达式文件的开头:C:\ Users \ Rod \ Desktop \ java files \ Lame.java   [line:154]错误:非法启动表达式文件:   C:\ Users \ Rod \ Desktop \ java files \ Lame.java [line:154]错误:';'   预期文件:C:\ Users \ Rod \ Desktop \ java files \ Lame.java [line:154]   错误:';'预期

2 个答案:

答案 0 :(得分:2)

请使用IDE(EclipseNetBeans?)来检查此类错误,即使像this这样的在线工具也足够了。 你的方法似乎太懒了,无法学习编程。这不是一个好的态度,一般而言,StackOverflow并不是要检查程序的语法,而是帮助解决问题。

您的错误主要与错放的}相关,请在此处查看// <---条评论

import java.util.Scanner;

public class Lame
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    int answer;

    System.out.println("Welcome to Rod's java game");
    System.out.println("Have fun!");
    System.out.println("*****RULES AND TIPS ARE IN THE FIRST AND ONLY COMMENT*****");
    System.out.println("You come to a fork in the road. Which way do you go?");
    System.out.println("Enter 1 for left or 2 for right");
    answer = input.nextInt();

    if (answer == 1)
    {
      Dead();
    }
    if (answer == 2)
    {
      Right();
    }
  }
  public static void Dead()
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Wrong decision, you died. Recompile and try a different path!");
  }
  public static void Right()
  {
    int answer1;

    Scanner input = new Scanner(System.in);
    System.out.println("");
    System.out.println("You walk down the right path and come to a peculiar house.");
    System.out.println("It's getting dark outside and the sign says beware of wolves.");
    System.out.println("Do you stay in the house for the night or sleep under a tree?");
    System.out.println("Enter 1 for the house or 2 for the tree");
    answer1 = input.nextInt();

    if (answer1 == 1)
    {
      House();
    }
    if (answer1 == 2)
    {
      Tree();
    }
  }
  public static void House()
  {
    int answer2;

    Scanner input = new Scanner(System.in);
    System.out.println("");
    System.out.println("You survived the night and continue your adventure!");
    System.out.println("");
    System.out.println("Beyond the house there is a man being robbed by a thug");
    System.out.println("What do you do?");
    System.out.println("Enter 1 to continue walking or 2 to help him");
    answer2 = input.nextInt();

    if (answer2 == 1)
    {
      Dead1();
    }
    if (answer2 == 2)
    {
      Help();
    }
  }
  public static void Tree()
  {
    int answer4;

    Scanner input = new Scanner(System.in);
    System.out.println("");
    System.out.println("You survived the night and continue your adventure!");
    System.out.println("");
    System.out.println("Beyond the house there is a man being robbed by a thug");
    System.out.println("What do you do?");
    System.out.println("Enter 1 to continue walking or 2 to help him");
    answer4 = input.nextInt(); // <----- CHANGE answer2 to 4

    if (answer4 == 1)
    {
      Dead1();
    }
    if (answer4 == 2)
    {
      Help();
    }
  } // <---- ADD PARENTHESIS HERE
    public static void Dead1()
    {
      Scanner input = new Scanner(System.in);
      System.out.println("As you attempt to continue walking, the thug stops you");
      System.out.println("Before you can say anything, he unsheaths his sword and kills               you!");
      System.out.println("Wrong decision, you died. Recompile and try a different path!");
    }
    public static void Help()
    {
      int answer3;

      Scanner input = new Scanner(System.in);
      System.out.println("");
      System.out.println("As you run over to help the man, the thug attacks you!");
      System.out.println("You defeat the thug easily, and he drops his Iron Sword!");
      System.out.println("****IRON SWORD OBTAINED****");
      System.out.println("");
      System.out.println("");
      System.out.println("You continue your journey.");
      System.out.println("After a few hours of walking, you get to a dark castle.");
      System.out.println("Your sense of adventure draws you towards the castle");
      System.out.println("You enter, and are immediately greeted by a spiral staircase.");
      System.out.println("Enter 1 to go up the staircase or 2 to continue walking");
      answer3 = input.nextInt();

      if (answer3 == 1)
      {
        Staircase();
      }
      if (answer3 == 2)
      {
        Continuewalking();
      }
     } // <---- ADD PARENTHESIS HERE
     public static void Staircase()
     {
       Scanner input = new Scanner(System.in);
       System.out.println("You die. The end.");
     }
     public static void Continuewalking()
     {
       Scanner input = new Scanner(System.in);
       System.out.println("You die. The end.");
     }
    //} <- REMOVE THEM
  //} <- REMOVE THEM
}

答案 1 :(得分:1)

您尝试在另一个方法(Dead1)内定义方法(main)。

它不起作用。从那里开始。

请记住,一致的缩进或多或少会消除这样的愚蠢错误。