-1

我有一门课可以测试放入自动售货机的金额。初始值存储在 txt 文件中。我正在尝试使用 printWriter 获取用户输入并将其存储在 txt 文件中。例如,一角硬币的起始数量是 5。用户输入 2 个一角硬币,文本文件中的新数量是 7。

这就是 Money.txt 的全部内容:

5 //nickels
5 //dimes
5 //quarters
0 //halfdollars

这是我的理财课程:public class Money

{
    private int nickels;
    private int quarters;
    private int dimes;
    private int halfs;


    public void increNickels() {
        nickels ++;
    }

    public int getNickels(){
        return nickels;
    }

    public void increQuarters(){
        quarters ++;
    }

    public int getQuarters(){
        return quarters;
    }

    public void increDimes(){
        dimes ++;
    }

    public int getDimes(){
        return dimes;
    }

    public void increHalfs(){
        halfs ++;
    }

    public int getHalfs(){
        return halfs;
    }
}

这是我的自动售货班:

    import java.io.*;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    import java.util.ArrayList;

    /**
     *
     * @author Mira and Ty
     */
    public class VendingClass {

            Money moneyObj;
            public Item [] itemList;
            public Money [] moneyList;
            Integer noCode = null;

            int itemChoiceNum;

        public VendingClass(){

            moneyObj = new Money();
            itemList = new Item [4];
            moneyList = new Money [4];
        }
        public void setItemList() throws IOException {

        String welcome = "Welcome to the \n \t\tPolemon Distribution Center \n \t\t\tPlease note that if there is not enough \nmoney in "
                    + "machine correct change \nwill not be given."; // creates welcome message

            JOptionPane.showMessageDialog(null, welcome);
            int itemQuantity; //variable that will display item code

            String items; //creates the String variable items which elements from itemNames array will be stored into
            int itemCost; //creates the integer variable itemCost whic elements from itemCosts array will be stored into

            File itemFile = new File("items.txt");
            Scanner itemScan = new Scanner(itemFile);

            int code = -1;
            for(int x = 0; x < itemList.length; x++){      //for loop addes the elements of each array and itemCode to itemList object
                items = itemScan.nextLine();
                itemCost = itemScan.nextInt();
                itemQuantity = itemScan.nextInt();
                itemScan.nextLine();
                code++;
                itemList[x] = new Item(code, items, itemCost, itemQuantity);
            }
            itemScan.close();
        }

    public void setMoney() throws IOException {

            File moneyFile = new File("Money.txt");
            Scanner moneyScan = new Scanner(moneyFile);

             for(int x = 0; x < moneyList.length; x++){
                int nickels = moneyScan.nextInt();
                int dimes = moneyScan.nextInt();
                int quarters = moneyScan.nextInt();
                int halfs = moneyScan.nextInt();
            }

            moneyScan.close();
    }



        public void displayVend(){

            String itemChoice = JOptionPane.showInputDialog(null, itemList);
                itemChoiceNum = Integer.parseInt(itemChoice);

            if(itemChoice == null){

                JOptionPane.showMessageDialog(null, "Thank you for your service. Goodbye!");
            }

                  JOptionPane.showMessageDialog(null, "You've caught a " + itemList[itemChoiceNum].getItem());


          }

        public void insertMoney() throws IOException{

            int moneyNeeded = itemList[itemChoiceNum].getCost();//price of item will be decremented

            while(moneyNeeded > 0){ //while loop testing the amount of money added to vending machine
            String message = JOptionPane.showInputDialog(null, "Pokemon: " + itemList[itemChoiceNum].getItem() + "\n You owe" + moneyNeeded + "\nEnter 50, 25, 10, or 5");

PrintWriter mw = new PrintWriter("Money.txt");            
int userMoney = Integer.parseInt(message);
            moneyNeeded = moneyNeeded - userMoney;

            if(userMoney == 5){
                mw.moneyObj.increNickels();
            }

            if(userMoney == 10){
                mw.moneyObj.increDimes();
            }

            if(userMoney == 25){
                mw.moneyObj.increQuarters();
            }

            if(userMoney == 50){
                mw.moneyObj.increHalfs();
            }

            if(message == null){
                JOptionPane.showMessageDialog(null, "Thank you for your service. Goodbye!");
                System.exit(0);
            }


           }
            JOptionPane.showMessageDialog(null, "Thank you for coming to the Pokemon Distribution Center! \nWe hope you and your " + itemList[itemChoiceNum].getItem() + " catch them all!");
    }


    }
4

2 回答 2

1

1)我刚刚测试了你的阅读文件逻辑,它不起作用。在某些时候,您将字符串视为 int。所以它失败了。看看我是如何从文件中提取值的:

        File moneyFile = null;

        Scanner moneyScan = null;

        int nickels = 0;
        int dimes = 0;
        int quarters = 0;
        int halfdollars = 0;        

        moneyFile = new File("Money.txt");

        try {
            moneyScan = new Scanner(moneyFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        String[] tempAr = new String[2];
        String temp = "";

        for(int i = 0; i<4; i++)
        {
            temp = moneyScan.nextLine();
            tempAr = temp.split("//");

            //match nickels
            if(tempAr[1].matches("nickels"))
            {
                nickels = Integer.parseInt(tempAr[0].trim());
                System.out.println(nickels);
            }

            //match dimes
            if(tempAr[1].matches("dimes"))
            {
                dimes = Integer.parseInt(tempAr[0].trim());
                System.out.println(dimes);
            }


            //match quarters
            if(tempAr[1].matches("quarters"))
            {
                quarters = Integer.parseInt(tempAr[0].trim());
                System.out.println(quarters);
            }

            //match halfdollars
            if(tempAr[1].matches("halfdollars"))
            {
                halfdollars = Integer.parseInt(tempAr[0].trim());
                System.out.println(halfdollars);
            }
        }

2)当您写入文件时,它保持空白,因为您没有刷新和关闭写入器流。如果不关闭流,它不会将字节刷新到文件,并且您的文件将保持空白。

IE: mw.close();

于 2013-05-05T01:06:02.617 回答
1

好的,我认为您的主要问题是,您实际上并没有告诉 PrintWriter 打印任何内容。

你有mw.moneyObj.increQuarters();和类似的功能,但他们不会自己做任何事情。

moneyObj.increQuarters();很好,但是您需要在其他地方调用单独的打印函数。

例如,您可能希望拥有:

void printAll(){
    mw.println(moneyObj.getQuarters());
    //Etc for all other printing.

}

当用户玩完自动售货机后,请他们保存结果(或拦截 JFrame 的关闭功能)。

然后按照您希望数字出现的顺序调用printAll()并打印出来。Money.txt

编辑 正如 Ravi 在下面提到的,您需要在方法.close()结束时调用printAll()以确保 printWriter 将正确写入文件。

于 2013-05-05T00:30:30.480 回答