Why does this Java program has infinites loops? -


this question has answer here:

why boolean conditions condition return true ? if variable reponse equal constants oui or non;

       final string oui = "o";        final string non = "n";        string reponse = oui;        // code omitted        {         // code omitted         // true        boolean condition = false;         {            system.out.println(msg_sol_troncon);            reponse = mscanner.nextline();            // debug            system.out.println("reponse:" + reponse + ":fin");            /*            // boucle infinie, problème avec la condition            // infinite loop            condition = !((reponse == non) || (reponse == oui));            system.out.println("condition : " + condition);            if (condition) {                system.out.println(msg_err_troncon);            } // if            */        } while(condition);      } while (reponse != non); 

on line have

condition = !((reponse == non) || (reponse == oui));

you need fix condition use string equality checks. == works when comparing numerical values , primitive data types. since strings separate class , non-primitive data type, must use string methods check equality. @bradimus saying, need like

condition = !((response.equals(non) || (response.equals(oui));

instead, since .equals string method

here's function documentation: https://docs.oracle.com/javase/7/docs/api/java/lang/string.html#equals%28java.lang.object%29


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -