Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Calculator{
- private String chooseOperand;
- // private boolean userAnswer;
- private Scanner userKeyboard;
- private boolean askOperand;
- private boolean runAskNumber;
- private boolean wantsUseCalculate;
- private boolean wantsToRerun;
- private int count;
- private double numbOne;
- private double numbTwo;
- private double operationTotal;
- public Calculator(){
- userKeyboard = new Scanner(System.in);
- askOperand = true;
- runAskNumber = false;
- }
- public void printIntro(){
- System.out.println("Hi, Welcome to Calculator");
- System.out.println("Please input what function you would like to do");
- System.out.println("If you would like to add, type: + ");
- System.out.println("If you would like to subtract, type: - ");
- System.out.println("If you would like to multiply, type: * ");
- System.out.println("If you would like to divide, type: / ");
- System.out.println("Then hit enter");
- }
- public void calculate(){
- System.out.println("In Calculate Method");
- do {
- while (askOperand == true){
- System.out.println("Please input what function you would like the Calculator to perform: ");
- chooseOperand = userKeyboard.nextLine();
- if (chooseOperand.substring(0).equalsIgnoreCase("*")){
- System.out.println("You want to multiply");
- askOperand = false; //ends current loop
- runAskNumber = true; //starts next loop to ask them for a number
- }
- else if(chooseOperand.substring(0).equalsIgnoreCase("/")){
- System.out.println("You want to divide");
- askOperand = false;
- runAskNumber = true;
- }
- else if(chooseOperand.substring(0).equalsIgnoreCase("+")){
- System.out.println("You Want to add");
- askOperand = false;
- runAskNumber = true;
- }
- else if(chooseOperand.substring(0).equalsIgnoreCase("-")){
- System.out.println("You want to subtract");
- askOperand = false;
- runAskNumber = true;
- }
- else{
- System.out.println("Please input either: * / + -");
- System.out.println("For example: *");
- }
- while (runAskNumber == true){
- System.out.println("What's your first number?");
- try{
- numbOne = userKeyboard.nextDouble();
- System.out.println("What's your second number?");
- numbTwo = userKeyboard.nextDouble();
- runAskNumber = false;
- }
- catch (Exception ex) {
- System.out.println("Please enter a number:");
- System.out.println("Example: 98");
- System.exit(1);
- }
- if (chooseOperand.substring(0).equalsIgnoreCase("*")){
- operationTotal = numbOne * numbTwo;
- System.out.println("Your Total is: " + operationTotal);
- wantsToRerun = true;
- }
- else if (chooseOperand.substring(0).equalsIgnoreCase("/")){
- operationTotal = numbOne / numbTwo;
- System.out.println("Your Total is: " + operationTotal);
- wantsToRerun = true;
- }
- else if (chooseOperand.substring(0).equalsIgnoreCase("+")){
- operationTotal = numbOne + numbTwo;
- System.out.println("Your Total is: " + operationTotal);
- wantsToRerun = true;
- }
- else {
- operationTotal = numbOne - numbTwo;
- System.out.println(operationTotal);
- wantsToRerun = true;
- }
- System.out.println("Would you like to do another calculation?");
- System.out.println("To do another calculation type: Yes");
- System.out.println("Quit the program type: No");
- System.out.println(" ------ ");
- userKeyboard.nextLine();
- /* ^ added because nextInt(); reads only one int and doesn't finish the line. So it reads the call after it asked for the 2nd number.
- https://stackoverflow.com/questions/7877529/java-string-scanner-input-does-not-wait-for-info-moves-directly-to-next-stateme */
- while (wantsToRerun == true){
- String userAnswer = userKeyboard.nextLine();
- if (userAnswer.contains("yes") || userAnswer.contains("Yes")){
- askOperand = true;
- wantsToRerun = false;
- }
- else if (userAnswer.contains("no") || userAnswer.contains("No")){
- wantsUseCalculate = false;
- }
- else {
- System.out.println("Please input either yes or no");
- }
- }
- }
- }
- } while(wantsUseCalculate == true);
- }
- public void printReport(){
- }
- public static void main(String args[]){
- Calculator myCalculator = new Calculator();
- myCalculator.printIntro();
- myCalculator.calculate();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement