我试图创建一个程序,该程序会向用户询问库中书籍的数量。然后它会请求那些书名并将它们存储在一个数组中。问题在于,当程序要求提供图书馆中的书籍数量时,该程序只需要索取图书名称一次。名称的用户输入
我有一种感觉,问题是在while循环内,并且扫描仪搞乱了,但我不确定。
import java.util.Scanner;
public class LIBBOOKS {
public static void main(String[] args)
{
int numB = 0;
int count = 0;;
boolean libEmpty = true;
String nameArray[];
String bookName = "";
System.out.println("Enter Number of Books: ");
Scanner value = new Scanner(System.in);
numB = value.nextInt();
nameArray = new String[numB];
while(libEmpty)
{
Scanner sValue = new Scanner(System.in);
System.out.println("Enter Book Name: ");
while(sValue.hasNextLine())
{
bookName = sValue.nextLine();
}
count++;
nameArray[count] = bookName;
if(numB < count)
{
System.out.println("Capacity Reached");
libEmpty = false;
}
sValue.close();
}
value.close();
System.out.println("Book Listing Entered:");
for(int i = 1; i > count; i++)
{
System.out.println(" " + nameArray[i]);
}
}
}
当我运行该程序,这是我所得到的:
Enter Number of Books:
4
Enter Book Name:
apples
corn
peaches
oranges
程序应该每个名字是什么之前,我有机会进入另一个名字时问我。 我希望发生:
Enter Number of Books:
4
Enter Book Name:
apples
Enter Book Name:
corn
Enter Book Name:
peaches
Enter Book Name:
oranges
Capacity Reached
Book Listing Entered:
corn apples peaches oranges
你能否把预期的产出呢? – Mritunjay
看来你可以使用一些免费的[调试技巧](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) –