Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class CalculatorDemo {
  4. public static void main(String[] args){
  5. Scanner console = new Scanner(System.in);
  6. String more = "no";
  7. do {
  8. System.out.println("Enter a number: ");
  9. System.out.print("x: ");
  10. double x = console.nextDouble();
  11. console.nextLine(); // read past the carriage return
  12. System.out.println("Now enter an operator to apply to that number");
  13. System.out.println("you can choose from sin, cos, tan, exp, log, sqrt");
  14. System.out.print("operator: ");
  15. String op = console.nextLine().trim(); // remove the spaces before and after the answer!
  16. double result;
  17. switch (op){
  18. case "sin": result=Math.sin(x); break;
  19. case "cos": result=Math.cos(x); break;
  20. case "tan": result=Math.tan(x); break;
  21. case "exp": result=Math.exp(x); break;
  22. case "log": result=Math.log(x); break;
  23. case "sqrt": result=Math.sqrt(x); break;
  24. default: result=0;
  25. }
  26. // you write the code to calculate the result of applying the operator to x
  27.  
  28.  
  29. System.out.printf("%s(%f)=%f%n",op,x,result);
  30. System.out.print("Another? (yes or no): ");
  31. more = console.nextLine().trim();
  32. } while (more.equals("yes"));
  33. System.out.println("Goodbye");
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement