Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要向简单票务系统添加用户确认和运行总数_Java_Netbeans - Fatal编程技术网

Java 需要向简单票务系统添加用户确认和运行总数

Java 需要向简单票务系统添加用户确认和运行总数,java,netbeans,Java,Netbeans,我必须完成一项我一直在做一些修改的当前作业,我被困在我希望在提交之前最后一个问题上 我需要为电影院创建一个简单的票务系统,并且我已经能够(在其他有经验的开发人员的帮助下)创建一个工作系统。但是,票务系统需要提示用户确认以继续该方法,并显示当前的总成本。确认需要是用户输入数字1,如果不是,则输出错误消息 我尝试再次使用=br.readLine()为了输出1以确认购买,我还尝试导入java.util.Scanner类,然后输入并使用int创建错误消息,但它仍然显示错误 package ticket

我必须完成一项我一直在做一些修改的当前作业,我被困在我希望在提交之前最后一个问题上

我需要为电影院创建一个简单的票务系统,并且我已经能够(在其他有经验的开发人员的帮助下)创建一个工作系统。但是,票务系统需要提示用户确认以继续该方法,并显示当前的总成本。确认需要是用户输入数字1,如果不是,则输出错误消息

我尝试再次使用
=br.readLine()
为了输出1以确认购买,我还尝试导入
java.util.Scanner
类,然后输入并使用
int
创建错误消息,但它仍然显示错误

 package ticketingsystem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ticketingsystem {

    enum TicketType {
// I chose to use the enum class for the ticket prices, //
// as it made it much easier to use a switch statement, instead of multiple if statements. //        
        CHILD(18), ADULT(36), SENIOR(32.5);
//Enum classes are needed to be used in upper case, otherwise the program will crash//
//as I discovered//        
        TicketType(double price) {
            this.price = price;
        }

        private double price;

        public double getPrice() {
            return price;
        }
    }

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //As the program needed the ability to run in an indefinite loop constantly adding //
        //calculations, I chose to use buffered reader, as it buffered the method each loop//
        //continuing to add a total number.//

        String type, again;
        int quantity = 0;
        // This is where the new calculation starts, it is set at 0 before being calculated and
        //added to the total price.//
        double totalPrice = 0;
        TicketType ticketType;
        //We link the TicketType calculations to the enum class, hence//
        // TicketType ticketType.//

        System.out.println("Welcome to the cinemas!");

        System.out.println("MAIN MENU\n");
        System.out.println("Cinema has the following ticketing options\n");
        System.out.println("1 = Child (4-5 yrs)");
        System.out.println("2 = Adult (18+ yrs)");
        System.out.println("3 = Senior (60+ yrs)");

        do {
            //Our loop calculation method starts here//
            System.out.print("\nEnter your option: ");

            type = br.readLine();

            switch (type.toLowerCase()) {
                case "1":
                    ticketType = TicketType.CHILD;
                    break;

                case "2":
                    ticketType = TicketType.ADULT;
                    break;

                default:
                    ticketType = TicketType.SENIOR;
                    break;
            }

            System.out.print("Enter total No of tickets: ");

            quantity = Integer.parseInt(br.readLine());

            totalPrice += ticketType.getPrice() * quantity;
            //totalPrice is the ticketType cost (hence the += operator), times//
            //the quantity.//
            System.out.printf("--> You are purchasing %s - %s Ticket(s) at $%s\n", quantity, ticketType, ticketType.getPrice());
            System.out.println("Press 1 to confirm purchase");

            //This is where we confirm the purchase//
            // This is where the current total cost needs to be output //
            System.out.print("\nDo you wish to continue?  (Y/N) : ");

            again = br.readLine();

        } while (again.equalsIgnoreCase("y"));
        //This is where the calculation method ends (as we are using a do/while loop).//
        // The while statement means that if we type "y", the loop will begin again with a buffer.//
        //If we type N, the loop will end, and the program will continue running past the loop.//
        System.out.printf("\n==> Total Price : $%s \n", totalPrice);
    }
}

如果您希望用户必须按
1
接受购买,那么在提出该问题后,只需输入一个简单的
If…else
。比如:

String confirmation = br.readLine();
if (confirmation.equals("1")) {
    totalPrice += ticketType.getPrice() * quantity;
    System.out.println("Current total is: " + totalPrice);
} else {
    System.out.println("You did not press 1 so ticket purchase cancelled");
    System.out.println("Current cost is still: " + totalPrice);
}
因此,只有当他们按下
1
时,它才会更新总数


但它仍然显示错误-错误是什么?我在当前的总成本系统中尝试了这个方法。out.print(“按1确认购买”);System.out.printf(“\n=>$%s\n”);还有一个sym错误我运行了你的代码,没有收到任何错误,只是你没有任何关于按1接受购买的信息。它只是一直接受购买。这是你的问题吗?几乎。。我需要在程序中添加一些内容,如何最好地编写到现有方法中,这让我感到困惑当您按1继续时,方法继续输出-输出显示当前的总票证成本-如果用户未按1,将显示错误消息并循环resetsAh图例谢谢!我尝试使用if语句,但没有尝试字符串确认。这就解决了我的问题,非常感谢:)