【问题标题】:Issue with for loop, taking in and utilising inputfor循环的问题,接受和利用输入
【发布时间】:2015-11-16 04:37:28
【问题描述】:

我正在尝试创建一个程序来接收商品代码并检查它们在“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.");
    }
}

【问题讨论】:

  • 在第二个 userInput.nextLine() 之前添加额外的 userInput.nextLine() 有帮助吗?让我知道它是否有效。
  • 你的意思是在这条线上吗? "字符串数量 = userInput.nextLine();"
  • 就在接收商品数量之前。 userInput.nextLine(); System.out.print("请输入商品数量:");字符串数量 = userInput.nextLine();
  • 我把它漏掉了,现在它进入代码底部的“---Purchase/Hire Items---”选项,但是当我输入 1 时,Purchasing... 是不输出。
  • 我编辑了我的帖子,我犯了一个描述问题的错误,它确实归结为“ ---购买/租用项目---”但是选择选项1时,它不起作用。

标签: java arrays file loops arraylist


【解决方案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"));

【讨论】:

  • 谢谢,它现在可以到达那里,但是当我尝试将“购买”代码更改为我的 Checkout 类中的方法调用时,它不会调用该方法。这是我现在在我的 Item 类中的代码,它应该链接到 Checkout。商品代码:pastebin.com/69k89NpY 结帐代码:pastebin.com/N5tB3hCj
猜你喜欢
  • 2014-01-03
  • 2021-11-17
  • 2013-08-31
  • 1970-01-01
  • 2022-11-15
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
相关资源
最近更新 更多