I've been working on this problem for 3 hours and I'm stuck
I have two conditions and I don't necessarily want to split them up using an if-else statement, what I mean by that is given this flow diagram:
I would need the if code to only be executed if its conditions are true and then to pass through the else code, so essentially the outgoing arrow of the if code would end on top of the else code, I know this is not the best in java as my textbook warns against it so maybe I can illustrate my problem with some code
int digit = -1;
do
{
System.out.print("Please enter your choise (1-6): ");
if (in.hasNextInt())
{
digit = in.nextInt();
}
else
{
in.next();
System.out.println(" ");
System.out.println("This is a wrong input. Please try again!");
}
}while (digit > 6 || digit < 1);
String digitName;
switch (digit)
{
case 1: digitName = "Hamburger"; break;
case 2: digitName = "Pizza"; break;
case 3: digitName = "Noodle"; break;
case 4: digitName = "Salad"; break;
case 5: digitName = "Sandwhich"; break;
case 6: digitName = "Finish the order!"; break;
default: digitName = ""; break;
}
System.out.println(" ");
System.out.println("Your choice is: " + digitName + ".");
if (digit == 6)
{
System.out.println("No need to pay for the delivery fee.");
System.out.println("The total price is $0.0.");
System.out.println("Thank you for using Online Order Program!");
}
else
{
System.out.print("Would you like to have some meat on your " + digitName + "? (Enter yes or no, don't worry about capitals) ");
String likeSomeMeatNul = in.next();
String likeSomeMeat = likeSomeMeatNul.toLowerCase();
if (!likeSomeMeat.equals("yes") && !likeSomeMeat.equals("no"))
{
do
{
System.out.println("\nThis is a wrong input. Please try again!");
System.out.print("Would you like to have some meat on your " + digitName + "? (Enter yes or no, don't worry about capitals) ");
String someMeat = in.next();
likeSomeMeat = someMeat.toLowerCase();
}while (!likeSomeMeat.equals("yes") && !likeSomeMeat.equals("no"));
}
if (likeSomeMeat.equals("yes"))
{
System.out.print("Beef or Pork? (Enter Beef or Pork, don't worry about capitals) ");
String beefOrPorkNul = in.next();
String beefOrPork = beefOrPorkNul.toLowerCase();
if (!beefOrPork.equals("beef") && !beefOrPork.equals("pork"))
{
do
{
System.out.println("\nThis is a wrong input. Please try again!");
System.out.print("Beef or Pork? (Enter Beef or Pork, don't worry about capitals) ");
String enterBeefOrPork = in.next();
beefOrPork = enterBeefOrPork.toLowerCase();
}while (!beefOrPork.equals("pork") && !beefOrPork.equals("beef"));
}
System.out.print("Would you like to have more food? (Enter yes or no, don't worry about capitals) ");
String moreFoodNul = in.next();
String moreFood = moreFoodNul.toLowerCase();
if (!moreFood.equals("yes") && !moreFood.equals("no"))
{
do
{
System.out.println("\nThis is a wrong input. Please try again!");
System.out.print("Would you like to have more food? (Enter yes or no, don't worry about capitals) ");
String enter_more_food = in.next();
moreFood = enter_more_food.toLowerCase();
}while (!moreFood.equals("yes") && !moreFood.equals("no"));
}
I thought it would check to see if the condition inside the if (like_some_meat.equals("yes")) is true or false, if true then execute and move on to the next line, if false move on to the next line, but this clearly isn't the case... it's looking for what happens if it's false and since I didn't put anything, the program terminates. I goes through if like_some_meat.equals("yes") is true however.
How do I get something to execute only if the condition is true and keep moving forward if false? If possible. I'm not sure my code is salvageable if I would have to split it into two separate branches.

Aucun commentaire:
Enregistrer un commentaire