0

I have a little difficulty writing code for my class, and I've been working on this one for about a week. I just can't get it to loop and redo the calculator block until the user types "Yes". I solved the running total problem I had, but no matter what I input when the calculator asks "Are you finished?", doesn't loop back and run the calculator again.

import java.util.Scanner;
public class Lab4InputRunningTotal
{
  public static void main (String[]args)
  {
int addTotal = 0;
int subTotal = 0;
int multiTotal = 0;
int divTotal = 0;
int remTotal = 0;
String finished;

double numberOne; // First user input
double numberTwo; // Second user input
char operator; // Operator Input

do // Start of calculator block
{
  System.out.println("Enter your first number"); // Prompt for first number

  Scanner number = new Scanner(System.in);
  numberOne = number.nextDouble();

  System.out.println("Enter your second number."); // Prompt for second number
  numberTwo = number.nextDouble();

  Scanner operatorIn = new Scanner(System.in); // Declare a char scanner to read the operator
  System.out.println("Enter an operation: +, -, *, /, %");
  operator = operatorIn.next().charAt(0);

  switch (operator) // Switch is neater and more compact than multiple if and else if statements.
  {
    case '+':   System.out.println("The sum of " + numberOne + " plus " + numberTwo + " is " + (numberOne + numberTwo)); // Addition
    addTotal += 1;
    break;

    case '-':  System.out.println("The difference of " + numberOne + " minus " + numberTwo + " is " + (numberOne - numberTwo)); // Subtraction
    subTotal += 1;
    break;

    case '*':  System.out.println("The product of " + numberOne + " times " + numberTwo + " is " + numberOne * numberTwo); // Multiplication
    multiTotal += 1;
    break;


    case '/': 
      if (numberTwo == 0.0)
      System.out.println("We're sorry. We cannot display the quotient of your numbers. \n  You cannot divide by zero, because that would implode the universe. "); // In case of zeroes
      else 
      {
        System.out.println("The quotient of " + numberOne + " divided by " + numberTwo + " is " + numberOne / numberTwo); // Division
        divTotal += 1;
      }
      break;

    case '%': 
      if (numberTwo == 0.0)
      System.out.println("We're sorry. We cannot display the remainder of your numbers. \n  You cannot divide by zero, because that would implode the universe. "); // Also in case of zeroes
      else
      {
        System.out.println("The remainder of " + numberOne + " divided by " + numberTwo + " is " + (numberOne % numberTwo)); // Remainder
        remTotal += 1;
      }
      break;


    default: System.out.println("Uh Oh. Something went wrong. Please try again.");
    break;
  }

  System.out.println("Are you finished with your calculations? Yes or No.");
  finished = operatorIn.next().toLowerCase();
}
while ( finished == (String)"no"); // End of calculator block

System.out.println( "You used addition " + addTotal + " times. \nYou used subtraction " + subTotal + " times. \nYou used multiplication " + multiTotal + " times. \nYou used division " + divTotal + " times. \nYou used remainder " + remTotal + " times.");




  }
}

And from there, I really don't know how to fix it.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Julian
  • 1

0 Answers0