为什么这些东西会出错?

时间:2015-07-14 18:52:42

标签: java

我是Java的新手,我一直在使用this tutorial作为模板。我不知道自己做错了什么,错误信息也很有帮助。

它说我需要至少10个声望来发布图片,所以我必须在这里复制它并告诉你哪些是带下划线的和错误信息:

  import java.util.Random; **Error: the "." after util.**
import java.util.Scanner; **Error: whole line underlined saying "Syntax error on token(s) misplaced construct(s)**

public class game {
private static final String[] enemies = null;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random rand = new Random();

String[] enemies = { "troll","rat","imp","goblin" };
int maxEnemyHealth = 80;
int enemyAttackDamage = 20;
int health = 100;
int playerAttackDamage = 30;
int numHealthPots = 3;
// int numStamimaPots = 3 (add stamina potions later on)
int healthPotionHeal = 20;
int healthPotionDropChance = 40; // 40%

boolean running = true;
System.out.println("Welcome, Dungeon Explorer!");

GAME:
while(running) {
System.out.println("-----------------------------------------------------");
}
}
private Scanner rand;
private int maxEnemyHealth;
int enemyHealth = rand.nextInt(maxEnemyHealth);
String enemy = enemies[rand.nextInt(enemies.length)];
System.out.println("\t#" + enemy + " attacked! #\n"); **Error: Everything after System.out is underlined red with the message: Syntax error on tokens, AnnotationName expected instead.**

while (enemyHealth > 0); { **Error: while, and the paranthesis after 0 is underlined red with the AnnotatioName message for while and Syntax error, insert ) to complete MethodDelcaration for the paranthesis** 
    System.out.println("HP:" + health);
    System.out.println("\t" + enemy + "'s +" " + HP: + " " + enemyHealth);
    System.out.println("\n\t What do you choose to do?" );
    System.out.println("\t Attack ");
    System.out.println("\t Health Potion");
    System.out.println("\t Run!");

    String input = in.nextLine();

帮助

2 个答案:

答案 0 :(得分:0)

第一个错误是由缺少的包声明创建的。要解决此问题,您可以将类移动到默认包(不推荐),也可以通过

添加包声明
package x.y.z;

位于班级的顶部,而x.y.z是您的包名。

第二个错误是由while语句

之后的第二个右括号引起的
while(running) {
System.out.println("-----------------------------------------------------");
}
} // This brace shall not be here

编译器将此大括号解释为关闭主方法,并且不再接受此大括号之后的方法。

然而,还有其他一些错误: - while语句后面的分号 - 局部变量声明之前的私有修饰符

答案 1 :(得分:0)

  1. 添加包声明。例如

    package game;
    

    或者,将其移至默认包。

  2. 通过在适当的位置添加}来关闭课程。最后是一个好的起点。

  3. 通过移除while (enemyHealth > 0); {并添加;来关闭循环来修复您的}循环。

  4. 将循环移至main方法。

  5. 同时将System.out.println("\t#" + enemy + " attacked! #\n");移至main方法。

  6. 修改此行中的引号System.out.println("\t" + enemy + "'s +" " + HP: + " " + enemyHealth);这将有效:

    System.out.println("\t" + enemy + "'s HP:" + enemyHealth);
    
  7. 现在,变量enemyenemyHealth也需要移入main方法。您的IDE可能会建议将它们设为静态。但是,由于Random rand = new Random();在main方法中被实例化,因此将它们设置为静态是没有意义的。

  8. 现在您的程序将运行。至于它是否真的做了它应该做的事情,我不能回答,因为你没有告诉我们。

    package game;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class Game {
    
        private static final String[] enemies = null;
        private Scanner rand;
        private int maxEnemyHealth;
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            Random rand = new Random();
    
            String[] enemies = { "troll", "rat", "imp", "goblin" };
            int maxEnemyHealth = 80;
            int enemyAttackDamage = 20;
            int health = 100;
            int playerAttackDamage = 30;
            int numHealthPots = 3;
            // int numStamimaPots = 3 (add stamina potions later on)
            int healthPotionHeal = 20;
            int healthPotionDropChance = 40; // 40%
    
            boolean running = true;
            System.out.println("Welcome, Dungeon Explorer!");
    
            GAME: while (running) {
                System.out
                        .println("-----------------------------------------------------");
            }
    
            int enemyHealth = rand.nextInt(maxEnemyHealth);
            String enemy = enemies[rand.nextInt(enemies.length)];
    
            while (enemyHealth > 0) {
                System.out.println("HP:" + health);
                System.out.println("\t" + enemy + "'s HP:" + enemyHealth);
                System.out.println("\n\t What do you choose to do?");
                System.out.println("\t Attack ");
                System.out.println("\t Health Potion");
                System.out.println("\t Run!");
    
                String input = in.nextLine();
            }
    
            System.out.println("\t#" + enemy + " attacked! #\n");
        }
    
    
    
    
    }