在java中使用导入时遇到问题

时间:2013-07-26 03:12:00

标签: java

我正在尝试编写一些以行开头的代码:

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() + ".");
        }           
    }
}

1 个答案:

答案 0 :(得分: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);

    }

}