Почему-то не смотря ни начто выполняет else
Почему-то, когда я ввожу "difficulty", оно кидает меня в "else", хотя я ввожу например "easy"
package com.company;
import java.util.Random;
import java.util.Scanner;
public class Game {
public static Random rand = new Random();
public static Scanner sc = new Scanner(System.in);
public static int number;
public static int tries;
public static int guess;
public static String difficulty;
public static boolean running = true;
public static void main(String[] args) {
NewGame();
while (true) {
WRAPup();
}
}
public static void NewGame() {
tries = 0;
System.out.println("Enter a difficulty");
System.out.println("easy (guess a number form 1 to 10), normal (guess a number form 1 to 50), hard (guess a number form 1 to 100)");
difficulty = sc.next();
// где-то здесь ошибка
if (difficulty == "easy") {
System.out.println("You picked a easy difficulty");
number = rand.nextInt(10);
} else if (difficulty == "normal") {
System.out.println("You picked a normal difficulty");
number = rand.nextInt(50);
} else if (difficulty == "hard") {
System.out.println("You picked a hard difficulty");
number = rand.nextInt(100);
} else {
System.out.println("You picked a else difficulty");
number = rand.nextInt(100);
}
//
}
public static void takeAguess () {
System.out.println("Ok, now take a guess");
guess = sc.nextInt();
tries += 1;
}
public static void body () {
if (guess > number) {
System.out.println("Your guess is greater than the number");
} else if (guess < number) {
System.out.println("Your guess is less than the number");
} else if (guess == number) {
tries += 1;
System.out.println("Thats right the number was " + number + " and it took you " + (tries - 1) + " tries");
NewGame();
}
}
public static void WRAPup () {
takeAguess();
body();
}
}
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
Строка - "ссылочный" тип.
if ("easy".equals(difficulty)) {
и так далее.