-2

我正在尝试从以下行开始编译一些代码:

import java.util.*;

我不断收到错误:

class, interface, or enum expected

我在其他帖子上看到这是因为我没有正确使用导入。我是否需要以某种方式进行设置,例如当我首先为 java 添加 PATH 异常时。我很难弄清楚这一点。我很确定代码除此之外还不错。

import java.util.*;

public class setUpGame {

        private ArrayList<Weapon> weaponArray = new ArrayList<Weapon>();

        class Weapon{
            private String name = null; 
            private String description = null;
            private int damageBase;

            public String getName(){
                return name;
            }
            public void setName(String n){
                name = n;
            }
            public String getDescription(){
                return description;
            }
            public void setDescription(String d){
                description = d;
            }
            public int getDamageBase(){
                    return damageBase;
            }
            public void setDamageBase(int d){
                damageBase = d;
            }
        }//END class Weapon

        Weapon spear = new Weapon();
        spear.setName("Mighty Spear");
        spear.setDescription("An ancient spear. Bronze of tip, ash of shaft, decorated in slivers or roc's feather.");
        spear.setDamageBase(3);

        Weapon hooks = new Weapon();
        hooks.setName("Silver Hooks");
        hooks.setDescription("A pair of sharp, hand-held fighting hooks. Obsidian core, plated in silver.");
        hooks.setDamageBase(2);

        Weapon flameSword = new Weapon();
        sword.setName("Flamesake");
        sword.setDescription("An arming sword, newly enchanted.  It erupts in flame when removed from the scabbard.");
        sword.setDamageBase(3);

        Weapon scissors = new Weapon();
        scissors.setName("Snippy");
        scissors.setDescription("Your mother's sewing scissors. They are nearly two hands long, as she was a large woman.");
        scissors.setDamageBase(1);

        Weapon darkGun = new Weapon();
        darkGun.setName("Dark Cannon");
        darkGun.setDescription("A finely crafted hand cannon.  It has been enchanted by a dark sorcerer to fire bolts of corrupting darkness.");
        darkGun.setDamageBase(3);

        Weapon iceBow = new Weapon();
        iceBow.setName("Chilling Bow");
        iceBow.setDescription("");
        iceBow.setDamageBase(2);

        weaponArray.add(spear);
        weaponArray.add(hooks);
        weaponArray.add(flameSword);
        weaponArray.add(scissors);
        weaponArray.add(darkGun);
        weaponArray.add(iceBow);



        // set up game flavor text
    }

public class SetUpGameTestDrive(){
    public static void main(String [] args){
        for (Weapon currentWeapon : weaponArray){
            System.out.println(currentWeapon.getName() + "is " + currentWeapon.getDescription() + "It has a damage base of " +
            currentWeapon.getDamageBase() + ".");
        }           
    }
}
4

1 回答 1

6

那应该是import java.util.*; 这个语句的意思是包括java.util包中存在的所有类型。

如果您只想包含特定类型,则可以使用完全限定名称,例如java.util.Calendar. 这将只包括Calendar类。

有一个常见的误解,即import语句用于包含在代码中使用的所需类型,但这并不完全正确。它只是使我们能够使用短名称而不是其完全限定名称。

你仍然可以使用

package com.so;

public class ImportTest {


    public static void main(String[] args) {

        java.util.Date date = new java.util.Date();
        System.out.println(date);

    }

}

这将编译并打印当前日期。该import语句仅使我们能够使用类型的短名称。

所以当你使用 import 语句时,代码就变成了。

包 com.so;

import java.util.Date;

public class ImportTest {


    public static void main(String[] args) {

        Date date = new Date();
        System.out.println(date);

    }

}
于 2013-07-26T03:13:26.900 回答