0

I need to create a java application where the user is asked if they want to continue or not after each question.

E.g What is your name? Do you want to continue? Y/N What pizza do you want to order?

So far my code is like this, But I don't know what to place in the if loop

public class Mod2P5{
public static void main(String[]args){

  while(true){


 Double cost = 0.00;
 String cont = "Y";
 /*Start of Menu*/
String pizza_item[] = new String [13];
pizza_item [0] = "Hawian";
pizza_item [1] = "Meat Lovers";
pizza_item [2] ="Vege";
pizza_item [3] = "Supreme";
pizza_item [4] = "Pepironi";
pizza_item [5] = "God Father";
pizza_item [6] ="Mr Wedge";
pizza_item [7] = "Double Bacon Cheese Burger";
pizza_item [8] = "Mustard Beef and Bacon";
pizza_item [9] ="Chilly Beef";
pizza_item [10] = "BBQ";
pizza_item [11] = "Sweet and Sour";
pizza_item [12] = "Prawn";
Double pizza_price[] = new Double [13];
pizza_price [0] =8.50;
pizza_price [1] = 8.50;
pizza_price [2] =8.50;
pizza_price [3] = 8.50;
pizza_price [4] =8.50;
pizza_price [5] = 8.50;
pizza_price [6] =8.50;
pizza_price [7] =8.50;
pizza_price [8] = 13.50;
pizza_price [9] =13.50;
pizza_price [10] = 13.50;
pizza_price [11] =13.50;
pizza_price [12] = 13.50;
 /*End of Menu*/ 


int pickup_delivery = readInt("Press 1 for delivery or 2 for pickup.");
cont = readString("Press Y to continue or N to cancel.");
String name = readString("What is your name");
cont = readString("Press Y to continue or N to cancel.");

  System.out.print(pickup_delivery + name + cost);

  if (cont.equalsIgnoreCase("Y")){
        break; 
// goes to beginning of while loop
  }
  }


}


/*reads and returns an integer from the keyboard*/
public static int readInt(String prompt){
System.out.println(prompt);
java.util.Scanner keyboard = new java.util.Scanner(System.in);
return keyboard.nextInt();
}
/*reads and returns a String from the keyboard*/
public static String readString(String prompt){
System.out.println(prompt);
java.util.Scanner keyboard = new java.util.Scanner(System.in);
return keyboard.nextLine();
}
/*reads and returns a double from the keyboard*/
public static double readDouble(String prompt){
System.out.println(prompt);
java.util.Scanner keyboard = new java.util.Scanner(System.in);
return keyboard.nextDouble();
}

}
Mat
  • 202,337
  • 40
  • 393
  • 406
Matt Ellwood
  • 105
  • 1
  • 13
  • If it's `homework`, please add a label. – Jared Farrish Jul 30 '12 at 03:08
  • Also, you say Java, where is the Javascript (as in your label)? – Jared Farrish Jul 30 '12 at 03:09
  • 2
    You have lots os problems with your question. You are using Java or JavaScript? You tagged like using JavaScript, but said that is Java in your question. You MUST not compare Strings using the == operator, but the equals method in Java. readInt and readStrings are methods that you defined? They are working properly? What do you mean with restart application? Close it and open again or just to restart questionaire? Please, improve your question and post n example that works (that can be compiled and executed with no errors). Doing it, we can use your code as a start point to help you. – davidbuzatto Jul 30 '12 at 03:09
  • Hi sorry, Yes readInt and ReadStrings are methods that get keyboard input. I just need it to start the questionnaire again from the beginning and reset all the variables. I have updated the question – Matt Ellwood Jul 30 '12 at 03:15
  • 3
    Please use a consistent and logical indent for code blocks. – Andrew Thompson Jul 30 '12 at 03:24
  • 1
    I agree with @Andrew. Your indentation and code formatting are awful and this will make it hard for you to debug your programs, and hard for us and your instructors to understand them. – Hovercraft Full Of Eels Jul 30 '12 at 03:46

3 Answers3

2

You can do something like this:

while(true) {
    // initialization

    int pickup_delivery = readInt("Press 1 for delivery or 2 for pickup.");
    cont = readString("Press Y to continue or N to cancel.");

    if (cont.equalsIgnoreCase("n"))
        continue; // goes to beginning of loop; restarts the questionaire
}

To get out the loop, just use break;.

Zong
  • 6,160
  • 5
  • 32
  • 46
  • Hi, I have tried that, but it only checks after I have answered all the questions, I need a way of resting the interations as soon as they answer N to a question. See the updated code – Matt Ellwood Jul 30 '12 at 03:28
  • Just check whenever you need to check then. Put the restart condition every time after you prompt the user if they want to cancel. – Zong Jul 30 '12 at 03:32
2

One thing that may be slipping you up -

if (cont=="n"){ //<-- this wont work 
  System.exit();
}

When comparing String objects, you need to use the equals() method - NOT the == operator. The == operator will compare references, and for String instance, that does not mean content.

@Zong Li mentions a better option in this context - using equalsIgnoreCase() which will make your program more user friendly.

akf
  • 38,619
  • 8
  • 86
  • 96
-1

Because it is home work, please, learn Design Pattern with Head First Design Pattern the easiest way to understand Objects. (or some other books or tutorial)

They told about pizza, and how to do have good practices ... and you will discover how restart works!

cl-r
  • 1,264
  • 1
  • 12
  • 26