我正在尝试创建一个程序,该程序接收项目代码并检查它们是否在“Stock.txt”文件中有效,格式如下:
ItemCode,ItemName,Quantity,Price
其内容为:
jmpr,Jumper,30,13.99
tsrt,T-Shirt,12,4.99
shs,Shoes,9,14.99
trsrs,Trousers,3,9.99
jkt,Jacket,0,19.99
scks,Socks,11,3.99
muwear,Male Underwear,4,4.99
fuwear,Female Underwear,8,6.99
hat,Hat,0,7.99
txdo,Tuxedo,3,99.99
我要求一个或多个以空格分隔的商品代码,然后他们希望购买的数量也用空格分隔,然后我将Stock.txt文件的内容存储到ArrayList中并扫描到检查项目代码是否存在以及该项目代码的相应数量是否小于或等于Stock.txt文件内部的数量,如果是,则将其添加到我将在稍后使用的String shoppingCart中上。如果数量大于当前库存量,如果物料代码错误,我也退出程序。
我遇到的问题是当我运行程序时,它会询问项目代码,然后是数量,它会在程序结束时返回打印语句,但是当输入1时,它会询问项目代码和数量再次,而不是打印出“采购......”,我不知道它不在循环内是什么错,任何帮助都会让我的一天。
*很抱歉,如果我把它拿出来放在自己的类中,代码中可能没有什么错误,因为我在其他文件中使用它作为一种方法。
import java.io.*;
import java.util.*;
public class Items
{
public static Scanner userInput = new Scanner(System.in);
public static File stockFile = new File("Stock.txt");
public static void main() throws IOException
{
boolean valid = true;
String shoppingCart = "";
List<String[]> contents = new ArrayList<>();
Scanner searchStockFile = new Scanner(stockFile);
while(searchStockFile.hasNextLine())
{
String[] current = searchStockFile.nextLine().split(",");
contents.add(current);
}
System.out.println("\n----------Add Items----------");
System.out.print("Enter Item Codes: ");
String itemCodes = userInput.nextLine();
String itemCodesArray[] = itemCodes.split(" ");
System.out.print("Enter Item Quantities: ");
String quantities = userInput.nextLine();
String quantityArray[] = quantities.split(" ");
int[] quantityIntArray = new int[quantityArray.length];
for(int i = 0; i < quantityArray.length; i++)
quantityIntArray[i] = Integer.parseInt(quantityArray[i]);
for(int i = 0; i < itemCodesArray.length; i++)
{
for(String[] elements : contents)
{
try
{
if(itemCodesArray[i].equals(elements[0]) && Integer.parseInt(elements[2]) >= quantityIntArray[i])
shoppingCart += itemCodesArray[i] + "," + elements[1] + "," + elements[2] + "," + quantityIntArray[i] + "," + elements[3] + ".";
else if(itemCodesArray[i].equals(elements[0]) && Integer.parseInt(elements[2]) < quantityIntArray[i])
{
System.out.println("The quantity of Item Code - " + itemCodesArray[i] + " - exceeds the current stock.");
valid = false;
}
}
catch(Exception e)
{
System.out.println("The Item code you entered does not exist.");
valid = false;
}
if(!valid)
System.exit(0);
}
}
System.out.println("\n----------Purchase/Hire Items----------");
System.out.print("1. Purchase Item\n2. Quit\nEnter Option Number: ");
String PH = userInput.nextLine();
if(PH.equals("1"))
System.out.println("Purchasing..)";
else if(PH.equals("2"))
System.out.println("Quitting..)";
else
System.out.println("Invalid Option chosen.\nPlease choose Options 1 or 2.");
}
}
答案 0 :(得分:1)
如果购买,您需要在用户输入后将其循环回来,请查看
boolean valid = true;
String shoppingCart = "";
List<String[]> contents = new ArrayList<>();
Scanner searchStockFile = new Scanner(stockFile);
while (searchStockFile.hasNextLine()) {
String[] current = searchStockFile.nextLine().split(",");
contents.add(current);
}
String PH = "1";
do {
System.out.println("\n----------Add Items----------");
System.out.print("Enter Item Codes: ");
String itemCodes = userInput.nextLine();
String itemCodesArray[] = itemCodes.split(" ");
System.out.print("Enter Item Quantities: ");
String quantities = userInput.nextLine();
String quantityArray[] = quantities.split(" ");
int[] quantityIntArray = new int[quantityArray.length];
for (int i = 0; i < quantityArray.length; i++) {
quantityIntArray[i] = Integer.parseInt(quantityArray[i]);
}
for (int i = 0; i < itemCodesArray.length; i++) {
for (String[] elements : contents) {
try {
if (itemCodesArray[i].equals(elements[0]) && Integer.parseInt(elements[2]) >= quantityIntArray[i]) {
shoppingCart += itemCodesArray[i] + "," + elements[1] + "," + elements[2] + "," + quantityIntArray[i] + "," + elements[3] + ".";
} else if (itemCodesArray[i].equals(elements[0]) && Integer.parseInt(elements[2]) < quantityIntArray[i]) {
System.out.println("The quantity of Item Code - " + itemCodesArray[i] + " - exceeds the current stock.");
valid = false;
}
} catch (Exception e) {
System.out.println("The Item code you entered does not exist.");
valid = false;
}
if (!valid) {
System.exit(0);
}
}
}
while (true) {
System.out.println("\n----------Purchase/Hire Items----------");
System.out.print("1. Purchase Item\n2. Quit\nEnter Option Number: ");
PH = userInput.nextLine();
if (PH.equals("1")) {
System.out.println("Purchasing..");
break;
} else if (PH.equals("2")) {
System.out.println("Quitting..");
System.exit(0);
} else {
System.out.println("Invalid Option chosen.\nPlease choose Options 1 or 2.");
}
}
} while (PH.equals("1"));