mardi 29 décembre 2015

Reading keystrokes from a keyboard and using if statement in Java [duplicate]

This question already has an answer here:

I am learning how to program in Java. I wrote the following code but, I can't get the if statements to work when the variable inputName is compared to a string and the statement is true. Does anyone have any tips on how to fix this?

import java.util.Scanner;
public class HelloWorld {

  public static void main(String[] args) {
    String x = ("Hello type in your first name.");
    System.out.println(x);
    Scanner keyboard = new Scanner(System.in);
    String inputName = keyboard.next();


    if (inputName == "erick") {
      System.out.println("Hi");
    }
    String v = "victor";
    if (inputName == v) {
      System.out.println("Hi");
    }
    String c = "christy";
    if (inputName == c) {
      System.out.println("Hi");
    }

    keyboard.close();
  }
}

Aucun commentaire:

Enregistrer un commentaire