根据用户输入修改数组列表 java

Modyifying an arraylist from user input java

使用我的代码,菜单运行。但是当用户输入水果的名称(保留它)时,我希望它说 'Reserved' 当用户然后按 'V' (去查看所有水果)

我已经阅读了 Oracle Java Collections Arraylist,根据我的理解,我必须使用 get 方法或 equals 方法进行编码,但我不太确定如何(虽然我尝试过)

请帮忙!

import java.util.ArrayList;
import java.util.Scanner;
public class FruitApril {
public static void main(String[] args) {

     TheMenu();  
 }
public static void TheMenu()
{
    String Customer[] = new String[10]; 
    Scanner input = new Scanner(System.in);
    ArrayList<String> fruits = initFruits();


    String option; 
    do {   // loop back to here as long as Q isn't selected           
    System.out.println("\nMenu");
    System.out.println("V: Views all fruit");
    System.out.println("R: To reserve fruit"); 
    System.out.println("Q: To exit");

    option = input.next();  

    if (option.charAt(0) == 'V' ) 
    { 
        viewAllFruit(fruits,Customer);
    } 


    if (option.charAt(0) == 'R' ) 
    { 
        reserveFruit(fruits,Customer);
    }

    }
    while (option.charAt(0) != 'Q');
    }   



    public static ArrayList<String> initFruits() {
    ArrayList<String> theFruit = new ArrayList<String>();
    theFruit.add("Plums");
    theFruit.add("Grapes");
    theFruit.add("Oranges");
    theFruit.add("Prunes");
    theFruit.add("Apples");
    return theFruit;
  }

 public static void viewAllFruit(ArrayList<String> fruits, String
 CustomerRef[])
  {
    for (String fruit : fruits) {
        System.out.println("Fruit " + fruit + " is in stock ");   
    }
  }

 public static void reserveFruit(ArrayList<String> fruits,String
  CustomerRef[])
{

    Scanner input = new Scanner(System.in);

    while(true) {
        System.out.println("Please enter the name of the fruit you'd like 
        to reserve");
        String Fruitn = input.next();

        if (Fruitn.equals(fruits))
        {
            fruits.get(Fruitn);
      }
   }
}

您好
我已经更改了您的代码

import java.util.ArrayList;
import java.util.Scanner;
public class FruitApril {
public static void main(String[] args) {

 TheMenu();  
}
public static void TheMenu()
{
String Customer[] = new String[10]; 
Scanner input = new Scanner(System.in);
ArrayList<String> fruits = initFruits();


String option; 
do {   // loop back to here as long as Q isn't selected           
System.out.println("\nMenu");
System.out.println("V: Views all fruit");
System.out.println("R: To reserve fruit"); 
System.out.println("Q: To exit");

option = input.next();  

if (option.charAt(0) == 'V' ) 
{ 
    viewAllFruit(fruits,Customer);
} 


if (option.charAt(0) == 'R' ) 
{ 
    reserveFruit(fruits,Customer);
}
if(option.charAt(0) == 'Q') // added this condition
{
    System.exit(0);
}

}while (option.charAt(0) == 'Q');

}

public static ArrayList<String> initFruits() {
ArrayList<String> theFruit = new ArrayList<String>();
theFruit.add("Plums");
theFruit.add("Grapes");
theFruit.add("Oranges");
theFruit.add("Prunes");
theFruit.add("Apples");
return theFruit;

}

 public static void viewAllFruit(ArrayList<String> fruits, String CustomerRef[])
  {
for (String fruit : fruits) {
    System.out.println("Fruit " + fruit + " is in stock ");   
}
  }

 public static void reserveFruit(ArrayList<String> fruits,String  CustomerRef[])
{

    Scanner input = new Scanner(System.in);

   while(true) {
    System.out.println("Please enter the name of the fruit you'd like to reserve");
    String Fruitn = input.next();
int index = fruits.indexOf(Fruitn);//added this line
    if (index>=0)// added this condition
    {
        String fruit = fruits.get(index); //added this line
    System.out.println("Reversed Fruit:"+fruit); // do according to your requirement
    }
  }
}
}

我从 user.Using 中获取了输入,我在 ArrayList 中找到了水果的索引,然后我使用 get 方法获取 fruit.You 没有提到你关于反转的要求那个字符串的。所以我只是根据您的要求输入 s.o.p.Complete 剩余的代码。

如果ArrayList是强制性的,我建议你为保留的水果创建另一个arraylist:

ArrayList<String> reservedFruits = new ArrayList<String>();

然后一旦一个水果被保留,只需将水果添加到这个列表中:

public static void reserveFruit(String fruitToReserve){
  reservedFruits.add(fruitToReserve);

  // you will want to remove the reserved fruit on your fruits Arraylist here
  // so that once you view the fruits, it wont be printed as available
  // i leave this method to you.
  removeTheReservedFruitFromTheList(fruitToReserve);
}

然后当客户查看您的列表(按 V)时,只需系统输出您的两个数组列表:

// print all available fruits
for(String fruit:fruits){
  System.out.println(fruit + " is available.");
}

// print all reserved fruits
for(String fruit:reservedFruits ){
  System.out.println(fruit + " is reserved.");
}

我给了您一些代码片段,但这应该能为您指明正确的方向。 希望这有帮助