while loop - simple calculation in java - let user know they have to use a valid operator -


i writing basic calculator program in java using joptionpane. new , problem having appreciated! have tried research online come across answers scanner imported or order of user input different mine.

my program suppose ask users number, has them select operator use (+,-,*,/,%) , asks users second number. after this, calculation performed , result displayed.

is possible use while loop (not while loop) check invalid operator before second number entered? have working code checks invalid operator after second number entered i'd find way check beforehand , re-ask users operator until valid 1 entered.

    while (!usecalculator) {      num1 = joptionpane.showinputdialog(null, "please enter first number:");     numone = double.parsedouble(num1);      input = joptionpane.showinputdialog(null, "thank you! do?\nplease choose following options:\n+\n-\n*\n%\n/");      num2 = joptionpane.showinputdialog(null, "thank you!\nplease enter second number:");     numtwo = double.parsedouble(num2);      if (input != null && input.equals("+"))     {add(numone, numtwo);     result = add(numone, numtwo); //i have separate mehtod     joptionpane.showmessagedialog(null, "the result of " + numone + " " + input + " " + numtwo + " is: " + result);     }     else if (input != null && input.equals("-"))     {minus(numone, numtwo);     result = minus(numone, numtwo);     joptionpane.showmessagedialog(null, "the result of " + numone + " " + input + " " + numtwo + " is: " + result);     }     else if (input != null && input.equals("*"))     {  multiply(numone, numtwo);    result = multiply(numone, numtwo);     joptionpane.showmessagedialog(null, "the result of " + numone + " " + input + " " + numtwo + " is: " + result);     }     else if (input != null && input.equals("/"))     { divide(numone, numtwo);    result = divide(numone, numtwo);     joptionpane.showmessagedialog(null, "the result of " + numone + " " + input + " " + numtwo + " is: " + result);     }     else if (input != null && input.equals("%"))      { modul(numone, numtwo);    result = modul(numone, numtwo);     joptionpane.showmessagedialog(null, "the result of " + numone + " " +     input + " " + numtwo + " is: " + result);     }      else  joptionpane.showinputdialog(null, "the operator invalid. please select correct operator.\nplease choose following options:\n+\n-\n*\n%\n/"); 

the thing is, want understand , learn how this. have added part of code guidance (i can add whole thing if needed) looking explanation on how accomplish can try out on own , work way through it. apologize if broad, i'd happy clear if needed! :)

thank all, t

just insert inner while @ line #6 follows:

while(input==null || !(input.equals("+") ||input.equals("*") || input.equals("/") || input.equals("-"))){     input = joptionpane.showinputdialog(null, "the operator has to either *,+,/,-") } 

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 -