Java:扫描程序/阵列列表类型有问题

Java:扫描程序/阵列列表类型有问题,java,arraylist,types,Java,Arraylist,Types,背景:我对编码非常陌生。我正在写一个基于文本的单人RPG作为一种学习方法 所以,我有一个ArrayList,用来存储类项的对象。我想根据用户从扫描器输入的信息检查ArrayList中是否存在项(item类中的对象) 如果可能的话,我认为如果我将一个项目传递给开关(基于用户输入),而不是一个字符串,我以后必须“翻译”ArrayList才能使用它,那么会更干净 可能吗?或者我必须按照我在下面代码中写出来的方式来做吗?或者有没有更好的,完全不同的方法,我不知道 public class Test{

背景:我对编码非常陌生。我正在写一个基于文本的单人RPG作为一种学习方法

所以,我有一个ArrayList,用来存储类项的对象。我想根据用户从扫描器输入的信息检查ArrayList中是否存在项(item类中的对象)

如果可能的话,我认为如果我将一个项目传递给开关(基于用户输入),而不是一个字符串,我以后必须“翻译”ArrayList才能使用它,那么会更干净

可能吗?或者我必须按照我在下面代码中写出来的方式来做吗?或者有没有更好的,完全不同的方法,我不知道

public class Test{

//New Array that will store a player's items like an inventory

static ArrayList<Item> newInvTest = new ArrayList<>();

//Placing a test item into the player's inventory array
//The arguments passed in to the constructor are the item's name (as it would be displayed to the player) and number of uses

static Item testWand = new Item("Test Wand", 5);

//Method for when the player wants to interact with an item in their inventory

public static void useItem(){
    System.out.print("Which item do you wish to use?\n: ");
    Scanner scanner5 = new Scanner(System.in);
    String itemChoice = scanner5.nextLine();
    itemChoice = itemChoice.toLowerCase();
    switch (itemChoice){
        case "testwand":
        case "test wand":
        boolean has = newInvTest.contains(testWand);
        if(has == true){
            //the interaction occurs
        }else{ 
            System.out.println("You do not possess this item: " + itemChoice);
        }
    }
}
公共类测试{
//新的数组将存储玩家的物品,如库存
静态ArrayList newInvTest=新ArrayList();
//将测试项目放入玩家的库存数组中
//传递给构造函数的参数是项的名称(将显示给播放器)和使用次数
静态项目测试棒=新项目(“测试棒”,5);
//当玩家想要与库存中的物品交互时的方法
公共静态void useItem(){
System.out.print(“您希望使用哪项?\n:”);
扫描仪scanner5=新扫描仪(System.in);
String itemChoice=scanner5.nextLine();
itemChoice=itemChoice.toLowerCase();
开关(项目选择){
案例“测试棒”:
案例“测试棒”:
布尔has=newInvTest.contains(testWand);
如果(has==true){
//相互作用发生了
}否则{
System.out.println(“您不拥有此项:“+itemChoice”);
}
}
}
提前感谢您的回答

表达式的类型必须是char、byte、short、int、Character、byte、short、Integer、String或枚举类型(§8.9),否则会发生编译时错误

这意味着您无法将项本身传递到交换机中。但是,如果您想要可读的代码,可能对于您的其他团队成员来说,如果您在一个组中工作,那么您可以为交换机使用hashmap和enum

例如:

public enum ItemChoice {
    SWORD, WAND, CAT
}
然后是hashmap

HashMap<String, ItemChoice> choice = new HashMap<String, ItemChoice>();
然后,您可以轻松地从用户输入中获取枚举值,然后在交换机中使用它。它比当前检查字符串的方法更广泛,但可读性更高,您可以称之为“cleaner”

如果您使用当前的方法,请使用字符串进行检查。然后我建议您从字符串itemChoice中删除“”空格,这样您就不必对案例执行任何操作,例如:

choice.put("Wand", ItemChoice.WAND)
case "testwand":
case "test wand":
但是你只需要一个箱子

case "testwand":
这实际上不会影响任何事情,但是既然不再使用布尔has,那么就可以这样做

if(newInvTest.contains(testWand)){
    // the interaction occurs
    // boolean has is not needed anymore!
}
对未来的建议是,你可能想创建一个玩家对象,这样你就可以在玩家对象中保存ArrayList,而不是一个静态变量。此外,它还可以让你更容易地保存玩家的数据,比如玩家拥有的钱数、击杀次数、关卡数等。最好尝试添加广告这里是面向对象编程

例如,你会:

public class Player {

    private ArrayList<Item> newInvTest = new ArrayList<>();

    private String name;

    private int currentLevel;

    public String getName(){
        return name
    }

    public int getCurrentLevel(){
        return currentLevel
    }

    public ArrayList<Item> getInventory(){
        return newInvTest;
    }

}

改用HashMap怎么样?你可以覆盖
equals
。标题不清楚,问题在于键入开关……你能详细说明保存数据的工作原理,以及如何将播放器作为类的对象(而不是我现在使用的带有静态变量的类)吗会有帮助吗?如果你的意思是为玩家属性保存数据。我已经发布了一个解释,说明了玩家类如何帮助组织和保存不同的值。你可能还想阅读:
player.getInventory();