Java 有没有办法从过去检索输入值

Java 有没有办法从过去检索输入值,java,Java,我是java的初学者,到目前为止,这是我唯一知道如何使用的代码。我正在为RPG游戏制作食物系统。基本上,它会显示一个可用食物的列表,并让你们按数字来吃。按下一个数字后,它会根据你的数字对应的内容打印出你决定吃什么。然后我需要检索你吃的“食物”,以便我可以使用它的统计数据。以下是我目前掌握的情况: public String eatmenu() { System.out.print ( "Consumables: "); for ( Sustenance consum : co

我是java的初学者,到目前为止,这是我唯一知道如何使用的代码。我正在为RPG游戏制作食物系统。基本上,它会显示一个可用食物的列表,并让你们按数字来吃。按下一个数字后,它会根据你的数字对应的内容打印出你决定吃什么。然后我需要检索你吃的“食物”,以便我可以使用它的统计数据。以下是我目前掌握的情况:

  public String eatmenu() {
    System.out.print ( "Consumables: ");
    for ( Sustenance consum : consumables ) {
        System.out.print ( "[" + consum + "], " );
    }
    int number = consumables.size();
    int counter = 1;

    while ( counter <= number ){
        System.out.print ("\nPress " + counter + " to consume " + consumables.get(counter-1));
        counter++;
    }
    int choice = reader.nextInt();
    String eatchoice = "You decided to consume " + consumables.get(choice-1);
    return eatchoice;
}
public String eat3(){

    //the food just eaten, 
}
}


对于如何使用eat3方法,欢迎提出任何意见。我知道我将使用toString方法打印出吃特定食物的效果,但我如何引用我刚吃的食物?我将把一切都当作批评。谢谢你抽出时间

用爪哇豆将食物凝固,然后取出。例如:

public class FoodBean{

public FoodBean(){}

private String foodName;
// other fields which you wana set or get

public void setFoodName(String foodName){
 this.foodName = foodName;
}

public String getFoodName(){
    return this.foodName;
  }
// override the toString() if you want the object to represent the foodName stored

@Override
public String toString(){
 return this.foodName;
  }
}
好的,现在我们有一个BeanClass

现在,您需要在用户单击任何项时创建一个bean对象

FoodBean fb = new FoodBean();
fb.setFoodName("get food name from the mapped list here against its number");
现在在程序中的任何地方使用getFoodName(),请小心,上面的bean对象具有局部作用域。如果在方法中创建它,则需要全局地对FoodBean进行相同的引用,并将新创建的对象分配给它,然后在类中的任何位置使用该全局引用。
进一步看一下这个简单的

使您的函数
eatmenu
返回所选的
维护
项,而不是考虑根据您的需要使用Bean类仔细说明@Sharpedge@user902383如果我那样做了,我会得到所有我称之为食物的东西吗?所以我会得出这样的结论:你的背包也轻了0磅,[罐装的酸使你的营养水平降低了0磅。你的背包也轻了0磅,[饼干使你的营养水平提高了0磅。你的背包也轻了0磅。],[玉米使你的营养水平提高了0。你的背包也减轻了0磅],[狼使你的营养水平提高了0。你的背包也减轻了0磅],[水果使你的营养水平提高了0…是的,等等,我会回答的
FoodBean fb = new FoodBean();
fb.setFoodName("get food name from the mapped list here against its number");