Java programming if to switch

This topic contains 3 replies, has 1 voice, and was last updated by  Amit 2 years, 6 months ago.

  • Author
  • #10925


    I’m trying to figure out how to make my java code from IFs to Switch. Can anyone help me?

    I’m trying to figure out how to put these into switch statements,

    Here’s my code,

  • #10926


    Make sure to change “String choice;” to “char choice”
    The reason you use a ‘ ‘ instead of ” ” is simply because you check for chars. Checking for chars must be done with ‘ ‘.

    If you want to keep it a String, change every ‘ in your switch to ”

    Also: I saw in your code this: reader.readLine(). I changed this to scan.nextLine() simply because I prefer to work with Scanner. It’s your choice though, you can re-edit it if you like.

  • #10927


    In C++ and C# we just put the name of the variable like this:

    (you change the “case ______” and add whatever you’d like, as long as it’s the same type of the variable declared)

  • #10928


    If i were to change the program so it loops back around to put another letter (choice) do I use continue ? I also need to figure out how to end the loop if I were to put a different letter besides the 3 options.

You must be logged in to reply to this topic.