Reputation: 59
I'm having trouble getting my code to return to the beginning of the code and restart when confirm is not equal to 1. I have tried the return function but it just repeats confirmed constantly and I'm not sure what to do.
import java.util.Scanner;
public class zoo {
public static void main(String[] args) {
int optionNumber = 0;
int numberOfTickets = 0;
int confirm = 0;
int totalAmount = 0;
int[] priceArray;
priceArray = new int[3];
priceArray[0] = 18;
priceArray[1] = 36;
priceArray[2] = (int) 32.50;
Scanner input = new Scanner (System.in);
System.out.println("\t" + "@@@@@ Welcome to Zoos Victoria @@@@@");
System.out.println("\t" + "\t" + "MAIN MENU" + "\n");
System.out.println("\t" + "Zoo has the following ticketing options" + "\n");
System.out.println("\t" + "1 = Child (4-6 yrs");
System.out.println("\t" + "2 = Adult (16+ yrs");
System.out.println("\t" + "3 = Senior (60+ yrs" + "\n");
System.out.println("Enter your option:" );
optionNumber=input.nextInt();
if(optionNumber == 1) {
System.out.println("Enter total No of tickets for Child");
numberOfTickets=input.nextInt();
} else if (optionNumber == 2){
System.out.println("Enter total No of tickets for Adult");
numberOfTickets=input.nextInt();
} else {
System.out.println("Enter total No of tickets for Senior");
numberOfTickets=input.nextInt();
}
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
if (confirm ==1) {
System.out.println("confirmed");
} else {
}
if(optionNumber == 1) {
totalAmount=priceArray[0]*numberOfTickets;
System.out.println("Total amount for child tickets: $" + totalAmount);
} else if (optionNumber == 2) {
totalAmount=priceArray[1]*numberOfTickets;
System.out.println("Total amount for adult tickets $" + totalAmount);
} else {
totalAmount=(int) ((double) priceArray[2]*numberOfTickets);
System.out.println("Total amount for senior tickets $" + totalAmount);
}
}
}
Upvotes: 0
Views: 8923
Reputation: 4266
Consider a do...while
loop, with the
while` checking the condition
For example:
Scanner input = new Scanner(System.in);
int confirm;
do {
// everything in here will repeat
// until the input is not equal to 1
// so put your desired code here
System.out.println("Press 1 to confirm");
confirm = input.nextInt();
} while (confirm != 1);
So everything within the {
after do
and until the }
before while
will repeat as long as the user does not enter 1
.
Upvotes: 2
Reputation: 396
You could build a loop around it. Something like this (removed some code, but the idea how it should look like should be clear):
boolean chooseOption = true;
while (chooseOption) {
Scanner input = new Scanner(System.in);
//print menu
System.out.println("\t" + "@@@@@ Welcome to Zoos Victoria @@@@@");
//TODO Your code
if (confirm == 1) {
System.out.println("confirmed");
// finish option selection
chooseOption = false;
} else {
//choseOption stays true, hence the menu will be reprinted
System.out.println("Cancelled ticketing." + " Selected Option number: " + optionNumber);
}
}
Always try to transfer your problem into a number of tasks. In this case you want to show a menu as long as no successfull selection was made.
Upvotes: 3