while loop - Simple calculator in java - using boolean to ask if user wants to continue -
i writing basic calculator program in java using joptionpane. new , have received far appreciated!
my program suppose ask users number, has them select operator use (+,-,*,/,%) , asks users second number. after this, calculation performed , result displayed.
i have working code loops calculator once calculation done, users asked if use again. code works adjust when user asked if want continue , yes, instead of loop going , having input box "welcome calculator!\nplease enter first number:") i'd "please enter first number:" , keep going (since user not need welcomed again).
public static void main(string[] args) { double numone; //first number float double numtwo; //second number float double result; //result of equation string num1; //first number string string num2; //second number string string input; //operator type (+,-,*,/,%) boolean usecalculator = false; //false quit, true use while (!usecalculator) { num1 = joptionpane.showinputdialog(null, "welcome calculator!\nplease enter first number:"); numone = double.parsedouble(num1); input = joptionpane.showinputdialog(null, "thank you! do?\nplease choose following options:\n+\n-\n*\n%\n/"); while (input == null || !(input.equals("+") || input.equals("*") || input.equals("/") || input.equals("-"))) { input = joptionpane.showinputdialog(null, "the operator invalid. please select correct operator.\nplease choose following options:\n+\n-\n*\n%\n/"); } num2 = joptionpane.showinputdialog(null, "thank you!\nplease enter second number:"); numtwo = double.parsedouble(num2); } }
and
public static boolean askyesnoquestion(string prompt) { string confirmationinput; boolean repeat = false; //false quite calc, true repeat use of calc while (true) { confirmationinput = joptionpane.showinputdialog(null, prompt + " (please answer yes or no)"); if (confirmationinput.equalsignorecase("y") || confirmationinput.equalsignorecase("yes")) { prompt = "please enter first number"; return true; } else if (confirmationinput.equalsignorecase("n") || confirmationinput.equalsignorecase("no")) { joptionpane.showmessagedialog(null, "you have decided quit calculator program.\n goodbye!"); return false; } if (!repeat) { prompt = "that invalid choice. " + prompt; repeat = true; } } }
same other questions, want understand , learn how this. have added part of code clarity (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! :)
@ajb hi everyone- have tried removing putting in changes (adding while loop , removing prompts) getting return error. have tried , finding has main method using void, unsure how fix this. if has further tips on new code below (so can again try , work through it), appreciate it!
import javax.swing.joptionpane; public class test { public static void main(string[] args) { double numone; //first number float double numtwo; //second number float double result; //result of equation string num1; //first number string string num2; //second number string string input; //operator type (+,-,*,/,%) string confirmationinput; boolean useagain = false; boolean usecalculator = false; //false if user decides quit, true if user continues use calculator while (!usecalculator) { num1 = joptionpane.showinputdialog(null, "welcome calculator!\nplease enter first number:"); numone = double.parsedouble(num1); input = joptionpane.showinputdialog(null, "thank you! do?\nplease choose following options:\n+\n-\n*\n%\n/"); //while loop gathers user input. asks user repeatedly until valid operator entered while (input == null || !(input.equals("+") || input.equals("*") || input.equals("/") || input.equals("-") || input.equals("%"))) { input = joptionpane.showinputdialog(null, "the operator " + input + " invalid. please select correct operator.\nplease choose following options:\n+\n-\n*\n%\n/"); } num2 = joptionpane.showinputdialog(null, "thank you!\nplease enter second number:"); numtwo = double.parsedouble(num2); //calculates result of user input based on operator choosen if (input != null && input.equals("+")) { add(numone, numtwo); result = add(numone, numtwo); 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); } if (!usecalculator) { joptionpane.showinputdialog(null, "would continue using calculator? (please answer yes or no)"); while (true) { if (confirmationinput.equalsignorecase("y") || confirmationinput.equalsignorecase("yes")) { num1 = joptionpane.showinputdialog(null, "please enter first number:"); return true; } else if (confirmationinput.equalsignorecase("n") || confirmationinput.equalsignorecase("no")) { joptionpane.showmessagedialog(null, "you have decided quit calculator program.\n goodbye!"); return false; } if (!useagain) { joptionpane.showinputdialog(null, "that invalid choice.\nwould continue using calculator? (please answer yes or no)"); useagain = true; } } } system.out.println("end of calculator program. program completed successfully"); } }
Comments
Post a Comment