import java.io.*;
import java.util.*;
import java.lang.*;
class MenuList
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
List<String> items=new ArrayList<String>();
int y;
do
{
//String n="";
//int i=0;
System.out.println("********************");
System.out.println(" MENU");
System.out.println("********************");
System.out.println("Press 1 to Add an Array with to List : ");
System.out.println("Press 2 to Remove an Array from the List : ");
System.out.println("Press 3 to Add Array at an Index in the List : ");
System.out.println("Press 4 to Replace an Array in the List: ");
System.out.println("Press 5 to Show the Output : ");
System.out.println("Press 6 to exit");
y=sc.nextInt();
//int [] arr= new int[5];
switch(y)
{
case 1:
System.out.println("Enter the Element to be added " );
items.add(sc.next());
break;
case 2:
System.out.println("Enter the Element's postion which you want to Remove " );
items.remove(sc.nextInt());
break;
在案例3和4中,我得到了与以前相同的错误。输入不匹配
case 3:
System.out.println("Enter the Elements postion and Element to be added : ");
items.add(sc.nextInt(),sc.next());
break;
索引从0开始递增。我输入4个值然后尝试 更改第二个索引处的值,它显示输入不匹配错误
case 4:
System.out.println("Enter the Elements postion and Element to be replaced :");
items.set(sc.nextInt(),sc.next());
break;
case 5:
System.out.println("Values you stored are as follows : " +items );
break;
case 6 :
break;
default:
System.out.println("You Have Entered Invalid Choice ");
}
}
while(y != 6);
}
}
答案 0 :(得分:1)
添加的第一个问题是显而易见的,不要添加&#39; n&#39;在: items.add(sc.next(n))的 你没有在下一行使用它,这是正确的
答案 1 :(得分:0)
我的代码正常运行,还有一些评论:
基本上,你需要移动
List<String> items=new ArrayList<String>();
在do
循环之外。您可以在每次迭代中重新创建它,而不是修改它。并使用sc.next()
代替sc.next(n)
来阅读字符串。