gpt4 book ai didi

java - 我正在尝试查找此代码中可能存在超出范围的变量的位置;它与文字游戏中的元素有关

转载 作者:行者123 更新时间:2023-12-01 09:14:33 25 4
gpt4 key购买 nike

错误发生在第二个片段中。请给点建议?

用于查看特定项目的代码

private void lookItem(Command command) 
{
if (!command.hasSecondWord())
{
// If there is no second word, we don't know where to look...
System.out.println("Look where?");
return;
}
else
{
// First check to see if that item is in the room...
// Need a loop to run through ArrayList and determine yes/no that the item is here
boolean presentInRoom = false;
for (int i=0; i<currentRoom.inventory.size(); i++)
{
if (currentRoom.inventory.get(i).getName().equalsIgnoreCase(command.getSecondWord()) == true)
{
presentInRoom = true;
break;
}
}

// If the item was present in the room's ArrayList, read or look at it
if (presentInRoom == true)
{
System.out.println ("You look at" );
}

// If the item was not present in the room's ArrayList print an error message
if (presentInRoom == false)
{
System.out.println( "isn't here!");
}
}
}

获取特定元素的代码

private void takeItem(Command command)
{
if (!command.hasSecondWord())
{
// if there is no second word, we don't know what to take...
System.out.println("Take what?");
return;
}
else
{
// First check to see if that item is in the room...
// Need a loop to run through ArrayList and determine yes/no that the item is here
boolean presentInRoom = false;
for (int i=0; i<currentRoom.inventory.size(); i++)
{
if (currentRoom.inventory.get(i).getName().equalsIgnoreCase(command.getSecondWord()) == true)
{
presentInRoom = true;
break;
}
}
}
// If the item was present in the room's ArrayList, add it to our own belongings (add
// to our own ArrayList, and delete from the ArrayList for this room
if (presentInRoom == true)
{
belongings.add(i);
inventory.remove(i);
System.out.println("You take" );
}

// If the item was not present in the room's ArrayList print an error message
if (presentInRoom == false)
{
System.out.println("isn't here!");
}
}

第二段代码标记出来

cannot find symbol - variable presentInRoom

当我尝试编译时。我还想知道如何在 println 中指定一个 Item。

最佳答案

在方法的开头声明 presentInRoom ,应该没问题(还为最后一个系统添加了 itemFound 字符串):

private void takeItem(Command command)
{
boolean presentInRoom = false;
String itemFound = "";
Integer itemId = null;
if(!command.hasSecondWord())
{
// if there is no second word, we don't know what to take...
System.out.println("Take what?");
return;
}
else
{
// First check to see if that item is in the room...
// Need a loop to run through ArrayList and determine yes/no that the item is here

for (int i=0; i<currentRoom.inventory.size(); i++)
{
if (currentRoom.inventory.get(i).getName().equalsIgnoreCase(command.getSecondWord()) == true)
{
presentInRoom = true;
itemFound = currentRoom.inventory.get(i).getName();
itemId = i;
break;
}
}
}
// If the item was present in the room's ArrayList, add it to our own belongings (add
// to our own ArrayList, and delete from the ArrayList for this room
if (presentInRoom == true)
{
belongings.add(itemId);
inventory.remove(itemId);
System.out.println("You take "+itemFound);
}

// If the item was not present in the room's ArrayList print an error message
if (presentInRoom == false)
{
System.out.println("isn't here!");
}
}

关于java - 我正在尝试查找此代码中可能存在超出范围的变量的位置;它与文字游戏中的元素有关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40664505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com