从用户输入java修改arraylist

时间:2015-04-14 00:49:34

标签: java eclipse methods arraylist menu

使用我的代码,菜单会运行。但是当用户输入水果的名称(保留它)时,我希望当用户按下“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);
      }
   }
}

2 个答案:

答案 0 :(得分:0)

您好我已更改您的代码

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
    }
  }
}
}

我已经从用户输入了表单。使用该输入我在ArrayList中找到了水果的索引然后我使用了get方法来获取该水果。你没有提到你对该字符串的反转的要求。所以我只是根据你的要求把sopComplete用于剩下的代码。

答案 1 :(得分:0)

如果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)时,只需系统输出两个arraylists:

// 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.");
}

我为您提供了代码段,但这应该指向正确的方向。 希望这有帮助