我是Java新手,我有一个问题。在Switch的情况下,如何在arrayList中添加值或索引?
我试过没有开关的情况,它会很好地工作,但在开关的情况下,根本不工作!!。我的代码运行,但我不能添加值,例如在情况1中。
这是我的密码。
import java.util.ArrayList;
import java.util.Scanner;
public class Eqla3tech {
public static void main(String[] args) {
menu();
}
static void menu(){
ArrayList<String> cars = new ArrayList<String>();
Scanner input = new Scanner(System.in);
System.out.println("Add Student <1>");
System.out.println("remove Student <2>");
System.out.println("show list of students <3>");
System.out.println("how many students? <4>");
int num = input.nextInt();
input.nextLine();
switch(num){
case 1:
System.out.println("write student name!!");
cars.add(input.next());
menu();
break;
case 2:
System.out.println("what number of index of student to remove");
int index = input.nextInt();
cars.remove(index);
menu();
break;
case 3:
System.out.println("Here's a list you have added.");
System.out.println(cars);
menu();
break;
case 4:
System.out.println("Here's haw many students");
System.out.println(cars.size());
menu();
break;
}
}
}
我试图做的是使数组成为动态的,并且我可以在不丢失值的情况下添加或删除Switch。
请为我糟糕的英语道歉。
我尝试过让用户输入,但没有成功。我的意思是,每次用户输入时,值都不会保存在addarrayList中。
我想我已经接近了,但我也输了。