dimanche 9 juillet 2017

What am I doing wrong here? Using an If / else if statement with Strings - Java [duplicate]

This question already has an answer here:

Forgive me for asking this question, I'm totally new to programming and I couldn't find it anywhere.

Just can't figure out what I'm doing wrong here


public class Primary {

public static void main(String[] args) {

    //Secondary Class
    Secondary secObj = new Secondary();
    secObj.conditionalOps();
}

}


import java.util.Scanner;

public class Secondary {

    public static void conditionalOps(){

        Scanner input = new Scanner(System.in);

        String sport;

        System.out.println("Please enter your favorite sport");
            sport = input.nextLine();

        if(sport == "hockey" || sport == "NHL" ){
            System.out.println("To find accessories for %s, please visit shop.nhl.com");
        }else if (sport == "basketball" || sport == "NBA"){
            System.out.println("To find accessories for %s, please visit store.nba.com ");
        }else if (sport == "baseball" || sport == "MLB"){
            System.out.println("To find accessories for %s, please visit www.mlbshop.com");
        }else if (sport == "football" || sport == "NFL"){
            System.out.println("To find accessories for %s, please visit www.nflshop.com");
        }else{
            System.out.println("That is not a sport we can help you with.");
        }


    }

}

This is the output I get regardless of what I do

Aucun commentaire:

Enregistrer un commentaire