我为学校制作了一个Java程序,允许客户购买火车票。到目前为止,一切工作都非常完美,除了当我调用final方法(当前的final方法最终会更多)时,它会打印我已命令打印的方法,但是它也会输出一些随机的文本行,我无法确定何时。 我将在下面粘贴程序代码,并且在运行该程序时也会显示控制台。我会尽力指出问题所在。非常感谢您,我希望有人能提供帮助。 ps。该程序应在“测试”处结束。
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购买Flexi门票
2
您已选择购买标准机票
为成人票输入1,为儿童票输入2:p
您选择了儿童票
请输入门票数量:
2
请输入您的目标区域(1、2、3、4):2
您购买的总价为€3.4
您想购买更多票吗?如果是,请输入1,否则请输入2:p
您已选择不购买更多票证
测试-需要程序在此处停止(临时)
***您输入的选项无效,请输入1或2
您已选择购买标准机票
为成人票输入1,为儿童票输入2:***
答案 0 :(得分:0)
由于您在打印测试后正在使用递归,因此方法调用的堆栈应使您回到:
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循环中以退出。
您还忘了在pay()方法之后休息一下。
这是修改后的代码:
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
}
}
希望这会对您有所帮助。