这是我的第一个程序,我遇到了这个问题。我想在用户输入ADD后将内容添加到列表中,在键入REMOVE后从列表中删除并检查当前状态。
我写了这个:
import java.util.*;
public class MainClass {
public static void main(String[] args) {
System.out.println("What's your name?");
Scanner scan = new Scanner(System.in);
String name;
name = scan.next();
System.out.println("Hello " + name);
ArrayList<String> list = new ArrayList<String>();
list.add("Ferrari La Ferrari");
list.add("Lamborghini Huracan");
list.add("Ducati 1199 Panigale");
list.add("Porshe 918");
list.add("McLaren P1");
list.add("Pagani Zonda F");
list.add("BMW M3 E92");
list.add("Audi R8");
list.add("Peugeot Oxia");
list.add("Citroen DS 5");
System.out.print("Now you have " + list.size());
System.out.println(" cars in the garage.");
System.out.println("");
System.out.println("ADD - to add vehicle.");
System.out.println("REMOVE - to delete vehicle.");
System.out.println("STATUS - to check current state of slots .");
String add;
add = scan.next();
if (add.equals("ADD")) {
System.out.println("Type name of vehicle: ");
list.add(add);
add = scan.next();
}
while (!add.equals("EXIT")) {
list.add(add);
add = scan.next();
}
System.out.println(list);
System.out.println("");
System.out.print("Currently you have " + list.size());
System.out.println(" cars in the garage");
System.out.println("");
scan.close();
}
}
我真的不知道如何解决这个问题。
答案 0 :(得分:0)
你需要保持ADD,EXIT&amp;在while循环中使用STATUS(保留,自己编写代码),如下所示
boolean temp =true;
while(temp){
System.out.println("ADD - to add vehicle.");
System.out.println("REMOVE - to delete vehicle.");
System.out.println("STATUS - to check current state of slots .");
add = scan.next();
if (add.equals("ADD")) {
System.out.println("Type name of vehicle: ");
add = scan.next();
list.add(add);
}
if(add.equals("EXIT")){
temp=false;
}
}
答案 1 :(得分:0)
尝试以这种方式实现删除部分。我希望它适合你。
if (add.equals("REMOVE")){
System.out.println("You can remove one of the following");
for (int i= 0; i < list.size(); i++){
System.out.println(i+1+". "+list.get(i));
}
int va = scan.nextInt();
System.out.println("The car <-"+list.remove(va-1)+"-> was successfully removed");
System.out.println("Thank you");
}
答案 2 :(得分:0)
检查下面的代码,我已经详细阐述了代码本身的所有重要内容。
在第一个代码块集中,我使用了set collection来存储车辆。
import java.util.*;
public class MainClass {
public static void main(String[] args) {
System.out.println("What's your name?");
Scanner scan = new Scanner(System.in);
String name;
name = scan.next();
System.out.println("Hello " + name);
HashSet<String> setOfvehicles = new HashSet<String>(); //List will have duplicate values, so if you use set it want have duplicate, Easy to remove
setOfvehicles.add("Ferrari La Ferrari");
setOfvehicles.add("Lamborghini Huracan");
setOfvehicles.add("Ducati 1199 Panigale");
setOfvehicles.add("Porshe 918");
setOfvehicles.add("McLaren P1");
setOfvehicles.add("Pagani Zonda F");
setOfvehicles.add("BMW M3 E92");
setOfvehicles.add("Audi R8");
setOfvehicles.add("Peugeot Oxia");
setOfvehicles.add("Citroen DS 5");
System.out.print("Now you have " + setOfvehicles.size());
System.out.println(" cars in the garage.");
System.out.println("");
System.out.println("ADD - to add vehicle."); //Add description
System.out.println("REMOVE - to delete vehicle."); //REMOVE description
System.out.println("STATUS - to check current state of slots ."); //STATUS description
System.out.println("EXIT - to exit.");//EXIT description
System.out.println("ADD/REMOVE/STATUS/EXIT");//Your system ask ADD/REMOVE/STATUS/EXIT form user
String userInput = scan.nextLine(); //user first input store here
while(userInput.compareToIgnoreCase("EXIT") != 0){ //Until user input is EXIT you will ask for input
if (userInput.compareToIgnoreCase("ADD") == 0) { //If user input is ADD
System.out.println("Type name of vehicle: ");//Ask for vehicle name to be add
String vehicleName = scan.nextLine();
setOfvehicles.add(vehicleName);
} else if(userInput.compareToIgnoreCase("STATUS") == 0){//If user input is STATUS
for(String setOfvehicle :setOfvehicles){
System.out.println(setOfvehicle);
}
} else if(userInput.compareToIgnoreCase("REMOVE") == 0){//If user input is REMOVE
System.out.println("Type name of vehicle to be removed: ");//Ask for vehicle name to be remove
String vehicleName = scan.nextLine();
setOfvehicles.remove(vehicleName);
}
System.out.println("ADD/REMOVE/STATUS/EXIT");//Your system ask ADD/REMOVE/STATUS/EXIT form user
userInput = scan.nextLine();//user next input store here
}
System.out.println("");
System.out.print("Currently you have " + setOfvehicles.size());
System.out.println(" cars in the garage");
System.out.println("");
scan.close();
}
}
如果您要将车辆存储在列表集合中,则删除此块。
else if(userInput.compareToIgnoreCase("REMOVE") == 0){//If user input is REMOVE
System.out.println("Type name of vehicle to be removed: ");//Ask for vehicle name to be remove
String vehicleName = scan.nextLine();
for(String setOfvehicle :setOfvehicles){
if(setOfvehicle.equals(setOfvehicle)){
setOfvehicles.remove(vehicleName);
}
}
}
如果您需要更多澄清评论。