让我的计算器循环直到用户告诉它停止?

时间:2016-03-14 23:22:33

标签: java loops input calculator do-while

我为我的班级编写代码有点困难,而且我已经在这个问题上工作了大约一个星期。我只是不能让它循环并重做计算器块,直到用户键入"是"。我解决了我遇到的总运行问题,但无论我在计算器询问时输入了什么,#34;你完成了吗?",不会再循环并再次运行计算器。

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.");




  }
}

从那里开始,我真的不知道如何解决它。

0 个答案:

没有答案