Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class CalculatorDemo {
- public static void main(String[] args){
- Scanner console = new Scanner(System.in);
- String more = "no";
- do {
- System.out.println("Enter a number: ");
- System.out.print("x: ");
- double x = console.nextDouble();
- console.nextLine(); // read past the carriage return
- System.out.println("Now enter an operator to apply to that number");
- System.out.println("you can choose from sin, cos, tan, exp, log, sqrt");
- System.out.print("operator: ");
- String op = console.nextLine().trim(); // remove the spaces before and after the answer!
- double result;
- switch (op){
- case "sin": result=Math.sin(x); break;
- case "cos": result=Math.cos(x); break;
- case "tan": result=Math.tan(x); break;
- case "exp": result=Math.exp(x); break;
- case "log": result=Math.log(x); break;
- case "sqrt": result=Math.sqrt(x); break;
- default: result=0;
- }
- // you write the code to calculate the result of applying the operator to x
- System.out.printf("%s(%f)=%f%n",op,x,result);
- System.out.print("Another? (yes or no): ");
- more = console.nextLine().trim();
- } while (more.equals("yes"));
- System.out.println("Goodbye");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement