Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:从一个基本类型创建多个独立的对象。文本冒险_Java_Object - Fatal编程技术网

Java:从一个基本类型创建多个独立的对象。文本冒险

Java:从一个基本类型创建多个独立的对象。文本冒险,java,object,Java,Object,在我的小文本游戏中,我有一个Item类,它从.txt文件加载对象,然后将对象添加到Item类型的ArrayList中。我正在尝试使用Item类中的upgrade()方法进行升级。它可以工作,但如果我的库存中有两个相同的项目,因为如果我升级一个项目,它们引用相同的项目,那么我升级该类型的所有项目 是否有一种方法可以创建一个新项目(青铜头盔),该项目在创建时与从文本文件加载的青铜头盔相同,但升级后不会更改Item.java中静态头盔阵列列表中的原始对象 播放器类中的openInventory()方法

在我的小文本游戏中,我有一个Item类,它从.txt文件加载对象,然后将对象添加到Item类型的ArrayList中。我正在尝试使用Item类中的upgrade()方法进行升级。它可以工作,但如果我的库存中有两个相同的项目,因为如果我升级一个项目,它们引用相同的项目,那么我升级该类型的所有项目

是否有一种方法可以创建一个新项目(青铜头盔),该项目在创建时与从文本文件加载的青铜头盔相同,但升级后不会更改Item.java中静态头盔阵列列表中的原始对象

播放器类中的openInventory()方法:

public void openInventory(){
        //newWindow();
        int selection;
        boolean upgraded = false;

            backPack.printInventory(this);
            println("[1] UseItem  [2] Upgrade Item  [3] Item Details");
            selection = getInt();
            if(selection == 1){
                println("Type the number of the corresponding Item Slot you would like to use.");
                    int itemNum = (getInt() - 1);
                    Item itemSelection = backPack.itemSlot[itemNum].oi;
                    if(backPack.itemSlot[itemNum].stack.size() > 0){
                        itemSelection = backPack.itemSlot[itemNum].stack.get(0);
                    }
                    itemSelection.printStats();
                    this.useItem(itemSelection);
            }
            if(selection == 2){

                int sInt;
                Item scroll = new Item();
                Item item = new Item();

                while(upgraded == false){
                    println("Pick a scroll");
                    sInt = getInt();
                    if(backPack.itemSlot[sInt - 1].oi.type.equals("scroll")){
                        scroll = backPack.itemSlot[sInt - 1].oi;
                    }
                    else
                        println("Not a Scroll");

                    println("Pick which " + scroll.category + " to upgrade.");
                    sInt = getInt();
                    if(backPack.itemSlot[sInt - 1].oi.type.equals(scroll.category)){
                        item = backPack.itemSlot[sInt - 1].oi;
                    }
                    else
                        println("Wrong type of Item");

                    if(scroll.name != "None" && item.name != "None"){
                        backPack.itemSlot[sInt - 1].oi.upgrade(scroll);
                        upgraded = true;
                    }
            }
        }
            if(selection == 3){
                println("Pick item to display details.");
                int i = getInt();

                backPack.itemSlot[i - 1].oi.printStats();
            }
    }
升级一个“青铜头盔”后的库存

—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
[1]  Bronze Helmet [+1]  [2]  Bronze Helmet [+1]  [3]  Helmet Def Scroll   [4]  None                [5]  None                

[6]  None                [7]  None                [8]  None                [9]  None                [10] None                
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
                                                        [Gold]:  $50
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
Item.java:

import static com.projects.aoa.Print.*;
import static com.projects.aoa.Input.*;

import java.util.*;
import java.io.*;

class Item {

    //Item
    String name, type, category, statUpgrade;
    int remSlots, maxSlots, upgraded, probability, statIncrease;
    int hp, mp, str, def, atk, dex, duration;
    Area shopPurchased;


    //Inventory
    public boolean filled;
    public int count, stackLimit;


    public static List<Item> helmets = new ArrayList<Item>();
    public static List<Item> potions = new ArrayList<Item>();
    public static List<Item> swords = new ArrayList<Item>();
    public static List<Item> scrolls = new ArrayList<Item>();

    Item(){
        name = "None";
        maxSlots = 5;
        remSlots = 5;
    }
    public String toString(){
        return (name);
    }

    static void loadItems(){

        try {
            //System.out.println(System.getProperty("user.dir"));

            FileInputStream fstream = new FileInputStream(System.getProperty("user.dir") 
                    + "/LoadFiles/" + "itemLoad.txt");

            DataInputStream in = new DataInputStream(fstream);

            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String line, itemType = "none";
            int counter = 0;
            Item newItem = new Item();

             while((line = br.readLine()) != null) {
                 if(line.length() == 0){
                        newItem.printStats();
                        if(itemType.equals("helmet")){
                            helmets.add(newItem);
                        }
                        if(itemType.equals("potion")){
                            potions.add(newItem);
                        }
                        if(itemType.equals("sword")){
                            swords.add(newItem);
                        }
                        if(itemType.equals("scroll")){
                            scrolls.add(newItem);
                        }
                        counter = -1;
                        }
                 if(line.equals("#"))
                     break;

                switch(counter) {
                    case -1:
                        counter++;
                        break;

                    case 0:

                        itemType = line;
                        counter++;
                        break;


                    case 1:
                        if(itemType.equals("helmet")){
                            //tempName = line;
                            newItem = new Helmet(line);
                            newItem.type = itemType;
                        }
                        if(itemType.equals("potion")){
                            newItem = new Potion(line);
                            newItem.type = itemType;
                        }
                        if(itemType.equals("sword")){
                            newItem = new Sword(line);
                            newItem.type = itemType;
                        }
                        if(itemType.equals("scroll")){
                            newItem = new Scroll(line);
                            newItem.type = itemType;
                            counter = 9;
                        }
                        counter++;
                        break;

                    case 2:
                        newItem.hp = Integer.parseInt(line);
                        counter++;
                        break;

                    case 3:
                        newItem.mp = Integer.parseInt(line);
                        counter++;
                        break;

                    case 4:
                        newItem.def = Integer.parseInt(line);
                        counter++;
                        break;

                    case 5:
                        newItem.str = Integer.parseInt(line);
                        counter++;
                        break;

                    case 6:
                        newItem.atk = Integer.parseInt(line);
                        counter++;
                        break;

                    case 7:
                        newItem.dex = Integer.parseInt(line);
                        counter++;
                        break;

                    case 8:
                        newItem.stackLimit = Integer.parseInt(line);
                        counter++;
                        break;

                    case 9:
                        newItem.duration = Integer.parseInt(line);
                        counter++;
                        break;
                    case 10:
                        newItem.category = line;
                        counter++;
                        break;
                    case 11:
                        newItem.statUpgrade = line;
                        counter++;
                        break;
                    case 12:
                        newItem.statIncrease = Integer.parseInt(line);
                        counter++;
                        break;
                    case 13:
                        newItem.probability = Integer.parseInt(line);
                        counter++;
                        break;
                }

             }

                in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    } 


    void printStats(){
        line();
        println("[" + name + "]");
        println("Type: " + type);
        println("Stack Limit: " + stackLimit);
        println("Duration: " + duration);
        println("HP:  " + hp);
        println("MP:  " + mp);
        println("Def: " + def);
        println("Str: " + str);
        println("Atk: " + atk);
        println("Dex: " + dex);
    }

    void upgrade(Item scroll){
        Random rand = new Random();

        int geti;

            if(remSlots > 0){
                if(this.type.equals(scroll.category)){
                    int prob = rand.nextInt(99);
                     println("Prob: " + prob);

                     if(prob < scroll.probability){
                         if(scroll.statUpgrade.equals("def")){
                             this.def += scroll.statIncrease;
                             this.upgraded++;
                             println("Upgraded: " + this.upgraded);
                         }
                     }

                     remSlots--;
                        println("Slots: " + this.remSlots + " / " + this.maxSlots);
                }
        }
            else
                println("No slots remaining.");
    }

static Item getItem(String searchedName){

        Item item = new Item();

        for(int i = 0; i < potions.size(); i++){
            if(potions.get(i).name.equals(searchedName)){
                item = potions.get(i);
                return item;
            }
        }
        for(int i = 0; i < helmets.size(); i++){
            if(helmets.get(i).name.equals(searchedName)){
                item = helmets.get(i);
                return item;
            }
        }
        for(int i = 0; i < swords.size(); i++){
            if(swords.get(i).name.equals(searchedName)){
                item = swords.get(i);
                return item;
            }
        }

        for(int i = 0; i < scrolls.size(); i++){
            if(scrolls.get(i).name.equals(searchedName)){
                item = scrolls.get(i);
                return item;
            }
        }
        return item;
    }
}
更改的附加项

void addItem(Item item){
        Item newItem = new Item(item);
        for (int i = 0; i < useableSlots; i++){
            if (itemSlot[i].occupied == false) {
                itemSlot[i].oi = newItem;
                itemSlot[i].occupied = true;
                break;
            }
        }
    }
void addItem(项目){
Item newItem=新项(Item);
对于(int i=0;i
我没有阅读所有的代码,但是
addItem
看起来可疑,可能会导致您的问题:

    void addItem(Item item){
    Item newItem = new Item();
    newItem = item;
    for (int i = 0; i < useableSlots; i++){
        if (itemSlot[i].occupied == false) {
            itemSlot[i].oi = newItem;
            itemSlot[i].occupied = true;
            break;
        }
    }
    }
这只是一个猜测,因为我不知道您实际上如何调用
addItem

)

编辑: 我刚才看到您正在
loadItems
中调用
addItem
。要么按照Tom Ingram的建议执行,要么更改
addItem
的实现。效果应该是一样的

编辑2: 如果您不打算子类化
,这将是一个快速解决方案:

 void addItem(){
 Item newItem = new Item();
 for (int i = 0; i < useableSlots; i++){
     if (itemSlot[i].occupied == false) {
         itemSlot[i].oi = newItem;
         itemSlot[i].occupied = true;
         break;
     }
 }
 }
void addItem(){
Item newItem=新项();
对于(int i=0;i
我没有阅读所有的代码,但是
addItem
看起来可疑,可能会导致您的问题:

    void addItem(Item item){
    Item newItem = new Item();
    newItem = item;
    for (int i = 0; i < useableSlots; i++){
        if (itemSlot[i].occupied == false) {
            itemSlot[i].oi = newItem;
            itemSlot[i].occupied = true;
            break;
        }
    }
    }
这只是一个猜测,因为我不知道您实际上如何调用
addItem

)

编辑: 我刚才看到您正在
loadItems
中调用
addItem
。要么按照Tom Ingram的建议执行,要么更改
addItem
的实现。效果应该是一样的

编辑2: 如果您不打算子类化
,这将是一个快速解决方案:

 void addItem(){
 Item newItem = new Item();
 for (int i = 0; i < useableSlots; i++){
     if (itemSlot[i].occupied == false) {
         itemSlot[i].oi = newItem;
         itemSlot[i].occupied = true;
         break;
     }
 }
 }
void addItem(){
Item newItem=新项();
对于(int i=0;i
而不是

Item newItem = new Item();

for ... 
  helmets.add(newItem);
难道不是吗

for ...
  helmets.add(new Item());

不如

Item newItem = new Item();

for ... 
  helmets.add(newItem);
难道不是吗

for ...
  helmets.add(new Item());


根据复制对象的推荐方法是提供复制构造函数

public Foo(Foo foo){
//.. copy all members
this.name=foo.name;
this.age=foo.age;

}
请注意,
clone
方法与同一本书相同

我认为您的方法是错误的,相反,您可以使用ObjectInputStream和ObjectOutputStream将对象写入数据文件

public class Item implements java.io.Serializable{
  ....
}

{
  Item item=new Item();
  ObjectInputStream stream=new ObjectInputStream(fileStream);
  stream.write(item);
}

根据复制对象的推荐方法是提供复制构造函数

public Foo(Foo foo){
//.. copy all members
this.name=foo.name;
this.age=foo.age;

}
请注意,
clone
方法与同一本书相同

我认为您的方法是错误的,相反,您可以使用ObjectInputStream和ObjectOutputStream将对象写入数据文件

public class Item implements java.io.Serializable{
  ....
}

{
  Item item=new Item();
  ObjectInputStream stream=new ObjectInputStream(fileStream);
  stream.write(item);
}

不要在库存中为同一物品设置两个对象,而是设置一个对象和一个计数器。(目录将告诉您该对象代表多少项)

而不是在库存中为同一项设置两个对象,即一个对象和一个计数器。(内容将告诉您该对象代表多少项)

请更具体一些,以便我们可以帮助您。问题对我来说不清楚,请您显示实际调用
upgrade()
method的代码对不起,忘了从Player类添加该方法……快5点了。如果您需要更多信息,请告诉我。@Director没有人会阅读所有这些代码。请更具体一些。请仔细阅读您的代码,并以最简单的方式告诉我们您的问题,以便我们可以帮助您。请更具体些,以便我们可以帮助您。问题对我来说不清楚,您能否显示实际调用
upgrade()的代码
method抱歉,忘记添加Player类中的方法了……快凌晨5点了。如果您需要更多信息,请告诉我。@Director没有人会阅读所有这些代码。请更具体一些。请仔细阅读您的代码,并以最简单的方式告诉我们您的问题,以便我们可以帮助您。NewItem实际上是我试图弄明白这一点的结果。我会将其编辑为行为相同的原始版本。我将尝试使用clone()。newItem实际上是我试图弄明白这一点的结果。我会将其编辑为行为相同的原始版本。不过我会尝试使用clone()。是的,Object的clone()方法提供了一个浅拷贝。但是,在您自己的类(项)中的clone()方法实现中,您可以执行与复制构造函数中相同的深度复制。是的,Object的clone()方法提供浅层复制。但是,在您自己的类(项)中的clone()方法实现中,您可以执行与复制构造函数中相同的深度复制。对于某些项,我这样做,但对于设备,一个项占用一个插槽。对于某些项,我这样做,但对于设备,一个项占用一个插槽。