-1

This is just something im trying out, i just recently started coding in java would like some help. Would like to ask user to pick their favorite movie, then take their input and use if statements to give different responses to each response.

import java.util.Scanner;
public class miniFFF {
public static void main (String[]p) {

    System.out.println("What is your favourite movie? pick from the answers below:");
    System.out.println("a");
    System.out.println("b");
    System.out.println("c");
    System.out.println("d");
    System.out.println("e");    

    answer();
    //ifStatements();
    System.exit(0);
    }

    public static String answer() {
        String favMovie;
        Scanner test = new Scanner(System.in);
        favMovie = test.next();

        if(favMovie == "a") {
            System.out.println("1234");
        }

        else if (favMovie == "b") { 
            System.out.println("123");
        }
        return favMovie;


}

}

2 Answers2

0

Using switch will make your code much readable.

switch (favMovie){
    case "a":  //do for a
        break;
    case "b" //do for b
        break;
    default: //no match
}

Java enum was designed for when you have a finite set of options. As good practice it will be a better option over string literal

Ndifreke
  • 732
  • 1
  • 8
  • 13
-1

I don't really see a question there and your code seems, I could point two thing I might do differently tho.

One I would use equalIgnoreCase instead of == in case someone doesn't put the correct capitalization (Also add a general case)

Also putting the method outside main seems to be better practice.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96