首页 > 解决方案 > 在java程序中得到意外的输出

问题描述

我为学校制作了一个 Java 程序,允许客户购买火车票。到目前为止一切正常,除了当我调用最终方法(当前最终方法最终会有更多)时,它会打印我命令该方法打印的内容,但它也会输出一些随机的文本行,我不知道何时。我将粘贴下面的程序代码,当我运行程序时,我还将显示控制台。我会尝试强调这个问题。非常感谢你,我希望有人能提供帮助。ps程序应该在“test”结束。

    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

    }
}

这是我运行程序时遇到的错误(没有以“测试”结尾,我不确定是什么原因造成的)

按 1 获取信息

按 2 购买标准票

按 3 购买 Flexi 门票

2

您已选择购买标准票

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

您选择了儿童票

请输入门票数量:

2

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

您购买的总价为 3.4 欧元

你想买更多的票吗?如果是,请输入 1,如果不是,请输入 2:2

您已选择反对购买更多门票

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

***您输入的选项无效,请输入 1 或 2

您已选择购买标准票

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

标签: java

解决方案


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

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 循环中以便退出。

你也忘了在 payment() 方法之后放 break。

这是修改后的代码:

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

    }
}

希望这会对你有所帮助。


推荐阅读