当前位置: 首页 > 知识库问答 >
问题:

在 java 程序中获取意外输出

澹台正真
2023-03-14

我为学校制作了一个javahtml" target="_blank">程序,允许客户购买火车票。到目前为止,一切都很完美,除了当我调用final方法(当前的final方法最终会有更多)时,它会打印我命令该方法打印的内容,但它也会输出一些随机的文本行,我无法确定何时。我将粘贴下面的程序代码,并在运行程序时显示控制台。我将努力突出问题。非常感谢,希望有人能帮忙。p、 程序应在“测试”结束。

    import java.util.Scanner; //imports the scanner class to allow for user input

public class ticketPurchase {
    //Adding global variables to be used by various methods
    static Scanner input = new Scanner(System.in);
    static String menuChoice;
    static double childTicket = 1.20;
    static double adultTicket = 2.50;
    static double adultFlexi = 12.00;
    static double childFlexi = 8.00;
    static String ticketType;
    static String ticketChoice;
    static int adultTicketQty;
    static int childTicketQty = 0;
    static String destinationZone;
    static double adultFinalPrice;
    static double childFinalPrice;


    public static void main(String[] args) {
        menu(); //calling the menu method within the main method to start the process
    }
    public static void menu() {
        System.out.println("Press 1 for information");
        System.out.println("Press 2 to purchase Standard Tickets");
        System.out.println("Press 3 to purchase Flexi tickets");
        menuChoice = input.next(); //allowing user to choose what tickets to buy or just to see information of services

        switch(menuChoice) { //switch statement to record and output information based on users input
        case "1":{ //prints information regarding pricing, ticket age restrictions and support
            System.out.println("The standard ticket may be a single or return ticket for an adult (16+) or a child");
            System.out.println("The flexi ticket covers all journeys for one 24 hour period for either a child or an adult");
            System.out.println("A single ticket's price depends on the journey length, single or return and if it is for an adult or a child");
            System.out.println("a Flexi ticket for a child costs €8.00 and a Flexi ticket for an adult costs €12.00");
            System.out.println("Our Customer Care telephone number for this terminal is 0830462920, please call if further support is required");
            break;
        }
        case "2":{
            ticketChoice = "standard"; //records the value of standard within the ticketChoice global variable
            chooseTickets(); //initiates the choose tickets method
            break;
        }
        case "3":{
            ticketChoice = "flexi"; //record the value of flexi within the ticketChoice global variable
            chooseTickets(); 
            break;
        }
        case "a":{ //allows user to enter the admin interface 
            admin();
            break;
        }
        default:{ //allows for user to input a value outside of the options and notify's them to try again
            System.out.println("Invalid choice, please choose from 1 to 3");
            break;
        }
        }
        menu(); //recursion
    }
    public static void chooseTickets() { //payment method for choosing adult or child and quantity, and purchasing.
        System.out.println("You have chosen to purchase " + ticketChoice + " ticket(s)");
        System.out.print("Enter 1 for an adult ticket, Enter 2 for a child ticket: "); 
        String ticketAgeGroup = input.next(); //assigns users choice to string variable
        switch(ticketAgeGroup) { //allows user to choose quantity and destination based on choice of adult or child
        case "1":{//case for adult tickets
            System.out.println("you have chosen adults ticket");
            ticketType = "adult";
            System.out.print("Please enter the quantity of tickets: ");
            adultTicketQty = input.nextInt();
            System.out.print("please enter your destination zone (1, 2, 3, 4): ");
            destinationZone = input.next();
            if(ticketChoice == "flexi") { //if statement to calculate the finalPrice variable value if the ticketChoice is Flexi
                adultFinalPrice = (adultFlexi*adultTicketQty);
            }
            else {
                adultFinalPrice = (adultTicket*adultTicketQty); //else calculates the finalPrice variable value if the ticketChoice is standard
            }
            switch(destinationZone){ // switch statement to calculate the final price depending on the destination's zone and their extra amount.
            case "1":{
                adultFinalPrice = adultFinalPrice + (adultTicketQty*0); 
                break;
            }
            case "2":{
                adultFinalPrice = adultFinalPrice + (adultTicketQty*.50); //calculation to add the extra amount for the destination
                break;
            }
            case "3":{
                adultFinalPrice = adultFinalPrice + (adultTicketQty*1.0);
                break;
            }
            case "4":{
                adultFinalPrice = adultFinalPrice + (adultTicketQty*1.50);
                break;
            }
            default:{
                System.out.println("you have entered an invalid choice please choose from 1 to 4");
                //recursion if the user enters an invalid choice 
                break;
            }
            } //end of the switch statement
            System.out.println("The total price for your purchase is €" + adultFinalPrice ); 

            System.out.print("Would you like to purchase more tickets? enter 1 if so, 2 if not: "); //allows user to purchase other tickets 
            String moreTicketChoice = input.next();
            if(moreTicketChoice.equals("1")) {
                chooseTickets();
            }
            else {
                System.out.println("you have chosen against purchasing more tickets");
            }
            payment(); //initiates the payment method after user has chosen quantity and ticket type etc
        }

        case "2":{ //case for children's tickets
            System.out.println("you have chosen children's ticket");
            ticketType = "child";
            System.out.println("Please enter the quantity of tickets: ");
            childTicketQty = input.nextInt();
            System.out.print("please enter your destination zone (1, 2, 3, 4): ");
            destinationZone = input.next();
            if(ticketChoice == "flexi") { //adjusts the price if user chooses the flexi option 
                childFinalPrice = (childFlexi*childTicketQty);
            }
            else {
                childFinalPrice = (childTicket*childTicketQty);
            }
            switch(destinationZone){ //adjusts price to account for the destination's zone chosen by user
            case "1":{
                childFinalPrice = childFinalPrice + (childTicketQty*0);
                break;
            }
            case "2":{
                childFinalPrice = childFinalPrice + (childTicketQty*.50);
                break;
            }
            case "3":{
                childFinalPrice = childFinalPrice + (childTicketQty*1.0);
                break;
            }
            case "4":{
                childFinalPrice = childFinalPrice + (childTicketQty*1.50);

                break;
            }
            default:{
                System.out.println("you have entered an invalid choice please choose from 1 to 4");
                chooseTickets();
                break;
            }
            }
            System.out.println("The total price for your purchase is €" + childFinalPrice );
            System.out.print("would you like to purchase more tickets? enter 1 if so, 2 if not: "); //allows user to purchase other tickets 
            String moreTicketChoice2 = input.next();
            if(moreTicketChoice2.equals("1")) {
                chooseTickets();
            }
            else if(moreTicketChoice2.equals("2")){
                System.out.println("you have chosen against purchasing more tickets");
            }
            payment();
        }
        default:{
            System.out.println("you have entered an invalid choice please enter 1 or 2");
            chooseTickets();
            break;
        }

        }

    }
    public static void payment() { //method to complete the payment process for the purchase
        System.out.println("test");

    }
    public static void printTickets() { //method to notify the customer that their tickets are printing 

    }
    public static void admin() { //method to control the admin's interface

    }
}

这是我运行程序时出现的错误(没有以“test”结尾,我不确定是什么导致的)

信息请按1

按2购买标准票

按3购买灵活门票

2

您已选择购买标准机票

成人票输入1,儿童票输入2:2

您选择了儿童票

请输入门票数量:

2

请输入您的目的地区域(1,2,3,4): 2

您购买的总价为3.4欧元

你想再买些票吗?如果是,输入1;如果不是,输入2:2

您已选择不购买更多门票

测试--需要程序在此停止(暂时)

* * *您输入了无效选项。请输入1或2

您已选择购买标准机票

输入 1 表示成人票,输入 2 表示儿童票:***

共有1个答案

锺威
2023-03-14

由于您在打印测试后使用递归,方法调用的堆栈应该让您回到:

case "2":{
        ticketChoice = "standard"; //records the value of standard within the ticketChoice global variable
        chooseTickets(); //initiates the choose tickets method
        break;
    }

在此之后,将再次调用menu()。因此,我建议您运行添加一个使用exit()的退出功能,或者将代码放在一个使用布尔标志的do while循环中,以便退出。

你也忘了把中断后支付()方法。

以下是修改后的代码:

import java.util.Scanner;

public class Testing {
    //Adding global variables to be used by various methods
    static Scanner input = new Scanner(System.in);
    static String menuChoice;
    static double childTicket = 1.20;
    static double adultTicket = 2.50;
    static double adultFlexi = 12.00;
    static double childFlexi = 8.00;
    static String ticketType;
    static String ticketChoice;
    static int adultTicketQty;
    static int childTicketQty = 0;
    static String destinationZone;
    static double adultFinalPrice;
    static double childFinalPrice;
    static boolean shouldBeRunningAgain = true;


    public static void main(String[] args) {
        menu(); //calling the menu method within the main method to start the process
    }
    public static void menu() {
        do {
            System.out.println("Press 1 for information");
            System.out.println("Press 2 to purchase Standard Tickets");
            System.out.println("Press 3 to purchase Flexi tickets");
            menuChoice = input.next(); //allowing user to choose what tickets to buy or just to see information of services

            switch (menuChoice) { //switch statement to record and output information based on users input
                case "1": { //prints information regarding pricing, ticket age restrictions and support
                    System.out.println("The standard ticket may be a single or return ticket for an adult (16+) or a child");
                    System.out.println("The flexi ticket covers all journeys for one 24 hour period for either a child or an adult");
                    System.out.println("A single ticket's price depends on the journey length, single or return and if it is for an adult or a child");
                    System.out.println("a Flexi ticket for a child costs €8.00 and a Flexi ticket for an adult costs €12.00");
                    System.out.println("Our Customer Care telephone number for this terminal is 0830462920, please call if further support is required");
                    break;
                }
                case "2": {
                    ticketChoice = "standard"; //records the value of standard within the ticketChoice global variable
                    chooseTickets(); //initiates the choose tickets method
                    break;
                }
                case "3": {
                    ticketChoice = "flexi"; //record the value of flexi within the ticketChoice global variable
                    chooseTickets();
                    break;
                }
                case "a": { //allows user to enter the admin interface
                    admin();
                    break;
                }
                default: { //allows for user to input a value outside of the options and notify's them to try again
                    System.out.println("Invalid choice, please choose from 1 to 3");
                    break;
                }
            }
        }while (shouldBeRunningAgain);
    }
    public static void chooseTickets() { //payment method for choosing adult or child and quantity, and purchasing.
        System.out.println("You have chosen to purchase " + ticketChoice + " ticket(s)");
        System.out.print("Enter 1 for an adult ticket, Enter 2 for a child ticket: ");
        String ticketAgeGroup = input.next(); //assigns users choice to string variable
        switch(ticketAgeGroup) { //allows user to choose quantity and destination based on choice of adult or child
            case "1":{//case for adult tickets
                System.out.println("you have chosen adults ticket");
                ticketType = "adult";
                System.out.print("Please enter the quantity of tickets: ");
                adultTicketQty = input.nextInt();
                System.out.print("please enter your destination zone (1, 2, 3, 4): ");
                destinationZone = input.next();
                if(ticketChoice.equals("flexi")) { //if statement to calculate the finalPrice variable value if the ticketChoice is Flexi
                    adultFinalPrice = (adultFlexi*adultTicketQty);
                }
                else {
                    adultFinalPrice = (adultTicket*adultTicketQty); //else calculates the finalPrice variable value if the ticketChoice is standard
                }
                switch(destinationZone){ // switch statement to calculate the final price depending on the destination's zone and their extra amount.
                    case "1":{
                        adultFinalPrice = adultFinalPrice + (0);
                        break;
                    }
                    case "2":{
                        adultFinalPrice = adultFinalPrice + (adultTicketQty*.50); //calculation to add the extra amount for the destination
                        break;
                    }
                    case "3":{
                        adultFinalPrice = adultFinalPrice + (adultTicketQty*1.0);
                        break;
                    }
                    case "4":{
                        adultFinalPrice = adultFinalPrice + (adultTicketQty*1.50);
                        break;
                    }
                    default:{
                        System.out.println("you have entered an invalid choice please choose from 1 to 4");
                        //recursion if the user enters an invalid choice
                        break;
                    }
                } //end of the switch statement
                System.out.println("The total price for your purchase is €" + adultFinalPrice );

                System.out.print("Would you like to purchase more tickets? enter 1 if so, 2 if not: "); //allows user to purchase other tickets
                String moreTicketChoice = input.next();
                if(moreTicketChoice.equals("1")) {
                    chooseTickets();
                }
                else {
                    System.out.println("you have chosen against purchasing more tickets");
                }
                payment(); //initiates the payment method after user has chosen quantity and ticket type etc
            }

            case "2":{ //case for children's tickets
                System.out.println("you have chosen children's ticket");
                ticketType = "child";
                System.out.println("Please enter the quantity of tickets: ");
                childTicketQty = input.nextInt();
                System.out.print("please enter your destination zone (1, 2, 3, 4): ");
                destinationZone = input.next();
                if(ticketChoice.equals("flexi")) { //adjusts the price if user chooses the flexi option
                    childFinalPrice = (childFlexi*childTicketQty);
                }
                else {
                    childFinalPrice = (childTicket*childTicketQty);
                }
                switch(destinationZone){ //adjusts price to account for the destination's zone chosen by user
                    case "1":{
                        childFinalPrice = childFinalPrice + (0);
                        break;
                    }
                    case "2":{
                        childFinalPrice = childFinalPrice + (childTicketQty*.50);
                        break;
                    }
                    case "3":{
                        childFinalPrice = childFinalPrice + (childTicketQty*1.0);
                        break;
                    }
                    case "4":{
                        childFinalPrice = childFinalPrice + (childTicketQty*1.50);

                        break;
                    }
                    default:{
                        System.out.println("you have entered an invalid choice please choose from 1 to 4");
                        chooseTickets();
                        break;
                    }
                }
                System.out.println("The total price for your purchase is €" + childFinalPrice );
                System.out.print("would you like to purchase more tickets? enter 1 if so, 2 if not: "); //allows user to purchase other tickets
                String moreTicketChoice2 = input.next();
                if(moreTicketChoice2.equals("1")) {
                    chooseTickets();
                }
                else if(moreTicketChoice2.equals("2")){
                    System.out.println("you have chosen against purchasing more tickets");
                }
                payment();
                break;
            }
            default:{
                System.out.println("you have entered an invalid choice please enter 1 or 2");
                chooseTickets();
                break;
            }

        }

    }
    public static void payment() { //method to complete the payment process for the purchase
        System.out.println("test");
        shouldBeRunningAgain = false;

    }
    public static void printTickets() { //method to notify the customer that their tickets are printing 

    }
    public static void admin() { //method to control the admin's interface

    }
}

希望这对你有所帮助。

 类似资料:
  • 我在JavaScript中使用fetch方法时遇到此错误。fetch问题:输入意外结束

  • 这是一个按升序排列句子中每个单词的程序。 这段代码对我来说似乎很完美,但是当我打印东西时,它显示了这个输出。 我真的不知道为什么会这样。 请帮忙。同时请提供一个替代方案。 我希望输出为

  • 我将项目更新为Xcode 5,并在构建设置中启用了模块。但是,当我使用时,我在程序中看到编译器错误

  • 问题内容: 我使用的是float常数,并将对象的私有float变量设置为下面的float常数,但是当对象输出设置为它的值时,它将四舍五入到float的最后一位。 输出:956.35626 对象中的变量声明为,下面是getter和setter。 知道为什么会这样吗? 问题答案: 由于单精度IEEE754浮点数仅具有23位精度(24位包括开始时的隐式1位)。 这大约等于七个十进制数字,您可以看到 您的

  • 嗨,我使用Spring mvc 3 和Hibernate 4 开发一个 Web 应用程序。我正在为所有配置使用注释。 当我尝试选择一个实体时,出现错误: 如果我用< code >进行查询。create SQL query() hibernate返回一个< code>ClanMember实体的< code>Object[]实例。 道中的方法: 实体: 表 CLAN_MEMBERS: 我做错了什么?先

  • 问题内容: 我编写了一个简单的Java程序,该程序每5秒就会向std输出一些“ hello”。 然后我编译它并得到一个.class。 我编写了另一个Java程序来运行它并获得输出: 但是它总是打印: 哪里错了?我的操作系统是“ Windows XP”。 问题答案: 到达流的末尾时将返回。 因为您基本上是在忽略此退出指示器并无限循环地循环,所以您得到的只是。 可能的原因是因为该进程已向错误流输出了一