gpt4 book ai didi

java - for 循环的问题,接收和利用输入

转载 作者:行者123 更新时间:2023-12-01 10:53:28 24 4
gpt4 key购买 nike

我正在尝试创建一个程序,该程序接受商品代码并检查它们在格式如下的“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

我要求提供 1 个或多个由空格分隔的商品代码,然后他们希望购买的数量也由空格分隔,然后我将 Stock.txt 文件的内容存储到 ArrayList 中并扫描它以获取检查商品代码是否存在,以及该商品代码的相应数量是否小于或等于 Stock.txt 文件内的数量,如果是,则它将添加到我稍后将使用的字符串购物车中在。如果数量大于当前库存量,并且如果商品代码错误,我将退出程序。

我遇到的问题是,当我运行程序时,它会要求输入项目代码,然后是数量,然后会进入程序末尾的打印语句,但是当输入 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.");
}
}

最佳答案

如果购买,需要在用户输入后循环返回,勾选此项

 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"));

关于java - for 循环的问题,接收和利用输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33728598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com