mardi 11 septembre 2018

Java: If statement not getting executed in a loop [duplicate]

This question already has an answer here:

I am working on a program in which at the end, I want to ask the user if he/she wants to continue or not. The program is in a loop. Here is the Code.

import java.util.*;
public class pc
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        String ch;
        for(int i = 1;i<=20;i++)
        {
            //Some Code....
            System.out.println("Do you Want to buy another item(Y or N)?");
            ch = sc.next();
            if(ch == "N"||ch == "n")
            {
                break;
            }
        }
    }
}

When I enter "N" in the terminal window, The program keeps on running and asking me to enter the value(till the for loop is done). I think the break statement is not executing, but why?

Aucun commentaire:

Enregistrer un commentaire