Java 添加arrayList.set的用户输入
我一直在做以下任务: 这个程序应该创建一个名为Book List的ArrayList。节目 应显示一个菜单,允许用户从以下选项中进行选择: 输入1将书本添加到列表中: 输入2可将书本编辑到列表中: 输入3从列表中删除一本书: 输入4以显示书籍列表: 输入5退出: 程序应使用case/switch语句并打开用户选择。程序应继续,直到用户输入5退出Java 添加arrayList.set的用户输入,java,arrays,Java,Arrays,我一直在做以下任务: 这个程序应该创建一个名为Book List的ArrayList。节目 应显示一个菜单,允许用户从以下选项中进行选择: 输入1将书本添加到列表中: 输入2可将书本编辑到列表中: 输入3从列表中删除一本书: 输入4以显示书籍列表: 输入5退出: 程序应使用case/switch语句并打开用户选择。程序应继续,直到用户输入5退出 案例1应使用BookList.add将书籍添加到ArrayList 案例2应使用BookList.set将书籍编辑到ArrayList 案例3应使用Bo
import java.util.ArrayList;
import java.util.Scanner;
public class BookList {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Scanner input = new Scanner(System.in);
//Create array
ArrayList<String> bookList = new ArrayList<String>();
//Add a few books to the array bookList
bookList.add("A Game of Thrones");
bookList.add("A Clash of Kings");
bookList.add("A Storm of Swords");
bookList.add("A Feast for Crows");
bookList.add("A Dance with Dragons");
//Display the items in bookList array and their indices.
System.out.println("******** The Book List ********");
for (int index = 0; index < bookList.size(); index++)
{System.out.println("Index: " + index + " Name: " + bookList.get(index));}
System.out.print("\n");
//declaring variables
int menuID = 0;
int changeIndex = 0;
int delIndex = 0;
String addTitle = "";
String corrTitle = "";
//Menu
System.out.println("Enter 1 to add a book to the list");
System.out.println("Enter 2 to edit a book in the list");
System.out.println("Enter 3 to remove a book from the list");
System.out.println("Enter 4 display the list of books");
System.out.println("Enter 5 to quit");
System.out.println("Enter a menu number (1-4 or 5 to Exit): ");
menuID = input.nextInt();
while (menuID != 0)
{
if (menuID >=1 && menuID <= 4)
{
switch (menuID)
{
case 1:
System.out.println("Enter the book to add: ");
addTitle = keyboard.nextLine();
bookList.add(addTitle);
System.out.print("\n");
System.out.println("******** The Book List ********");
for (int index = 0; index < bookList.size(); index++)
{System.out.println("Index: " + index + " Name: " + bookList.get(index));}
break;
case 2:
System.out.println("Enter index number of the book to change: ");
changeIndex = keyboard.nextInt();
System.out.println("Enter the corrected book name: ");
corrTitle = keyboard.nextLine();
bookList.set(changeIndex, corrTitle);
System.out.println("******** The Book List ********");
for (int index = 0; index < bookList.size(); index++)
{System.out.println("Index: " + index + " Name: " + bookList.get(index));}
break;
case 3:
System.out.println("Enter index number of the book to remove: ");
delIndex = keyboard.nextInt();
bookList.remove(delIndex);
System.out.println("******** The Book List ********");
for (int index = 0; index < bookList.size(); index++)
{System.out.println("Index: " + index + " Name: " + bookList.get(index));}
break;
case 4:
System.out.println("******** The Book List ********");
for (int index = 0; index < bookList.size(); index++)
{System.out.println("Index: " + index + " Name: " + bookList.get(index));}
break;
case 5:
System.out.println("Goodbye!");
break;
}}
else if (menuID >=1 && menuID <= 4)
System.out.println("You must enter a number 1-5:");
System.out.println("Enter a menu number (1-4 or 5 to Exit): ");
menuID = input.nextInt();
}
}
}
import java.util.ArrayList;
导入java.util.Scanner;
公共课书目{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
扫描仪输入=新扫描仪(System.in);
//创建数组
ArrayList bookList=新建ArrayList();
//向数组图书列表中添加一些图书
书单。添加(“权力游戏”);
书单。添加(“国王的冲突”);
书单。加上(“刀剑风暴”);
书单。添加(“乌鸦的盛宴”);
增加(“与龙共舞”);
//显示图书列表数组中的项目及其索引。
System.out.println(“*********图书列表*******”);
对于(int index=0;index 如果(menuID>=1&&menuID=1&&menuID您应该只使用一个具有相同源的扫描器对象,除非您有一种非常复杂的方法来遍历输入,这里可能不是这样。因此您的代码应该如下所示:
// Scanner keyboard = new Scanner(System.in);
Scanner input = new Scanner(System.in);
// ...
menuID = input.nextInt();
while (menuID != 0)
{
if (menuID >=1 && menuID <= 4)
{
switch (menuID)
{
case 1:
// ...
addTitle = input.nextLine();
// ...
menuID = input.nextInt();
input.nextLine(); // Consumes the rest of the line
menuID = Integer.parseInt(input.nextLine());
或者,您可以每次获取整行并使用integer.parseInt
解析其中包含的整数,如下所示:
// Scanner keyboard = new Scanner(System.in);
Scanner input = new Scanner(System.in);
// ...
menuID = input.nextInt();
while (menuID != 0)
{
if (menuID >=1 && menuID <= 4)
{
switch (menuID)
{
case 1:
// ...
addTitle = input.nextLine();
// ...
menuID = input.nextInt();
input.nextLine(); // Consumes the rest of the line
menuID = Integer.parseInt(input.nextLine());
使用第二个选项(我发现在这种情况下更好)
作为旁注,您要求用户使用5
退出,但只要menuID
不是0
,您的while
将继续运行。请阅读:。不要将整个代码都转储到我们身上。您了解了方法吗?如果了解,您应该创建一个方法,例如名为listBooks()
,因此不必重复打印图书列表的逻辑4次。