所以我的程序运行正常,但是当我输入带空格的项时,它会抛出java.util.InputMismatchException。所以我将所有kb.next()更改为kb.nextLine(),但我遇到的问题是例如:添加部分,一旦打印请输入您要添加的标题。 ,它不允许我输入任何内容,它只是跳到下一行并打印请输入您要添加的项目的有值。
System.out.println( "Please enter the title that you would like to add." );
title = kb.next();
System.out.println( "Please enter the want value of the item that you would like to add." );
wantValue = kb.nextInt();
System.out.println( "Please enter the have value of the item that you would like to add." );
haveValue = kb.nextInt();
package assignment3;
import java.util.Scanner;
public class Question1
{
static SortedList inventory;
//main
public static void main(String[] args)
{
inventory = new SortedList();
@SuppressWarnings("resource")
Scanner kb = new Scanner(System.in);
String command, title;
int wantValue, haveValue;
do
{
System.out.println("\n MAIN MENU");
System.out.println( "H. Provide a summary of available commands");
System.out.println( "L. List the entire inventory." );
System.out.println( "I. Display Inventory information for a specific title.");
System.out.println( "A. Add a new title to Inventory.");
System.out.println( "M. Modify the want value of a specific title.");
System.out.println( "S. Sell an item");
System.out.println( "O. Write a purchase order for additional DVD's");
System.out.println( "R. Write a return order.");
System.out.println( "Q. Quit the program.");
command = kb.next();
//If the user need a summary of available commands.
if(command.equalsIgnoreCase("H"))
{
System.out.println( "H (help), provides a summary of available commands." );
System.out.println( "L (list), list the entire inventory." );
System.out.println( "I<title> (inquire), Display theinventory information for a specific title." );
System.out.println( "A<title> (add), add a new title to the existing inventory." );
System.out.println( "M<title> (modify), modify the want value of a specific title." );
System.out.println( "S<title> (sell), sell an item, decrease have value of that item by 1." );
System.out.println( "O (order), write an order for additional items so have value = want value." );
System.out.println( "R (return), return extras so have value = want value." );
System.out.println( "Q (quit), quit the program." );
}
//If the user want the inventory info on a specific title
else if(command.equalsIgnoreCase("I"))
{
System.out.println( "Please enter the title that you would like to inquire." );
title = kb.next();
//Search through the inventory
for(int i =1; i<=inventory.size();i++)
{
StockItem item = (StockItem)inventory.get(i);
//if a matching title is found
if((item.getTitle()).equals(title))
{
//print out the item
String s= item.toString();
System.out.println(s);
}
}
}
//If the user want a list the entire inventory.
else if(command.equalsIgnoreCase("L"))
{
//Search through the inventory
for(int i =1; i <=inventory.size(); i++)
{
StockItem item = (StockItem)inventory.get(i);
//print out the item
String s= item.toString();
System.out.println(s);
}
}
//If the user want to add a new item to the inventory
else if(command.equalsIgnoreCase("A"))
{
System.out.println( "Please enter the title that you would like to add." );
title = kb.next();
System.out.println( "Please enter the want value of the item that you would like to add." );
wantValue = kb.nextInt();
System.out.println( "Please enter the have value of the item that you would like to add." );
haveValue = kb.nextInt();
//Add the new item to the inventory
StockItem NewItem = new StockItem(title,haveValue,wantValue);
inventory.sortedAdd(NewItem);
String s= NewItem.toString();
System.out.println(s);
}
//If the user want to change an item in the inventory
else if(command.equalsIgnoreCase("M"))
{
System.out.println( "Please enter the title of the DVD you would like to modify" );
title = kb.next();
//Search through the inventory
for(int i =1; i<=inventory.size();i++)
{
StockItem item = (StockItem)inventory.get(i);
//If an item is found, print it out and ask the user for a new want value
if((item.getTitle()).equals(title))
{
System.out.println( "Title: " + item.getTitle());
System.out.println( "Want value: " + item.getWant());
System.out.println( "Have value: " + item.getHave());
System.out.println( "Please enter the new want value for " + item.getTitle());
int newWantValue = kb.nextInt();
item.setWant(newWantValue);
String s= item.toString();
System.out.println(s);
}
}
}
//If the user want to sell an item for the inventory
else if(command.equalsIgnoreCase("S"))
{
System.out.println( "Please enter the title of the DVD you would like to sell" );
title = kb.next();
//Search through the inventory
for(int i =1; i<=inventory.size();i++)
{
StockItem item = (StockItem)inventory.get(i);
if((item.getTitle()).equals(title))
{
//if there is non in the inventory, add the user to the waiting list
if(item.getHave() == 0)
{
System.out.println( "We are currently out of " + item.getTitle());
System.out.println( "Please add customer to waiting list");
System.out.println( "Please enter customer's last name");
String lastName = kb.next();
System.out.println( "Please enter customer's first name");
String firstName = kb.next();
System.out.println( "Please enter customer's phone number");
String phone = kb.next();
item.addToWaitingList(lastName,firstName,phone);
}
else{
//decrease the have by 1
item.setHave(item.getHave()-1);
}
String s= item.toString();
System.out.println(s);
}
}
}
//if the user want to print out a purchase order
else if(command.equalsIgnoreCase("O"))
{
//String s = " Purchase Order: \n";
System.out.println("Purchase Order:");
for(int i =1; i<=inventory.size(); i++)
{
StockItem item = (StockItem)inventory.get(i);
if(item.getHave()<item.getWant())
{
int ToBePurchased = item.getWant()+item.getSizeofWaitingList()- item.getHave();
String s = "\n Title: "+ item.getTitle() +"\t"+"Number in stock: "+ item.getHave()+"\t"+"Number of Wants:"+item.getWant()+"\t"+" Number of customers on waiting List : "
+item.getSizeofWaitingList()+"\t"+"Number to be Purchased: " +ToBePurchased ;
System.out.println(s);
}
}
}
//if the user want to a return order
else if(command.equalsIgnoreCase("R"))
{
//Search through the inventory
for(int i =1; i<=inventory.size();i++)
{
StockItem item = (StockItem)inventory.get(i);
if(item.getHave()>(item.getWant()+item.getSizeofWaitingList()))
{
int returnValue = item.getHave() - item.getWant() - item.getSizeofWaitingList();
item.setHave(returnValue);
String s= item.toString();
System.out.println(s);
}
if(item.getHave() == 0 && item.getWant() == 0 && item.getSizeofWaitingList() == 0)
{
inventory.sortedRemove(item);
String s= item.toString();
System.out.println(s);
}
}
}
} while (!command.equalsIgnoreCase("Q"));
}
}
答案 0 :(得分:0)
我知道了,我在每个需要用户输入的if语句后添加了一个新的输入扫描器扫描仪输入=新的扫描仪(System.in); 。而不是全球性的。