dimanche 16 décembre 2018

Comparing in JAVA

I do not know, whether I am blind or stupid, but I do not see the reason why my while cycle does not work.

Please see the first "If condition" in while loop and the comments. Shortly: If I put "k" from the keyboard, this condition is FALSE and I do not know why? After that the while cycle is not ending, because k!="k" ???

Could anyone help please?

Thank you,

M.

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    Zasobnik_dyn z = new Zasobnik_dyn();//stack
    String akce = "z";  //action = zacatek(start)

    while(akce != "k") { //action is not k = end
        System.out.println("Akce: (p=pridej(add),o=obsluz(serve),k=konec(end))");
        akce = sc.next();       //I´ve put "k" from the keyboard
        System.out.println(akce);

        if (akce == "k") { //here "FALSE" - why??? and the while cycle is not ending
            System.out.println("Breaking...");
            break;
        }


        System.out.println("Zakaznik cislo: ");
        int zakaznik = sc.nextInt();
        if(akce == "p") {
            z.push(zakaznik);
        }
        else if (akce == "o") {
            z.pop();
        }
        z.vypisPole();
    }

Aucun commentaire:

Enregistrer un commentaire