dimanche 6 novembre 2016

I've to add the statement if the user wants to continue (y/n) in each operation. Can someone help me out?

import java.util.Scanner;

public class Calculator {

 public static void main(String[] args) {
  Scanner userInput = new Scanner(System.in);
  String operator;
  double num1, num2, answer = 9;
  System.out.println("Enter first number: ");
  num1 = userInput.nextDouble();
  System.out.println("Enter operator: ");
  operator = userInput.next();
  System.out.println("Enter second number: ");
  num2 = userInput.nextDouble();
  if (operator.equals("+")) {
   answer = num1 + num2;
  } else if (operator.equals("-")) {
   answer = num1 - num2;
  } else if (operator.equals("*")) {
   answer = num1 * num2;
  } else if (operator.equals("/")) {
   answer = num1 / num2;
  } else if (operator.equals("^")) {
   answer = Math.pow(num1, num2);
  }
  System.out.println("First number:" + num1);
  System.out.println("Operator:" + operator);
  System.out.println("Second number:" + num2);
  System.out.println("Answer: " + answer);
 }
}

Aucun commentaire:

Enregistrer un commentaire