0

I want add sin cos tan pow please help me

// Created by Dark light import java.util.Scanner; public class calculater { public static void main(String[ ] args) { Scanner input=new Scanner (System .in); System .out.println ("Enter 1st num"); int a=input.nextInt(); System .out.println ("Enter operation"); String op=input.next(); System .out.println ("Enter 2stnum"); int b=input.nextInt(); //int z; if(op.equals("+")){ System.out.println(a+b); } else if(op.equals("-")){ System.out.println(a-b); } else if(op.equals("*")){ System.out.println(a*b); } else if (op.equals("/")){ System.out.println(a/b); } //System .out.println ("The result is"+ z); } }

6th Feb 2021, 8:54 PM
Dark Light
Dark Light - avatar
2 odpowiedzi
+ 3
You could do something like this: import java.util.Scanner; public class calculater { private static boolean isOneOperandOperation(String op) { return op.equals("sin") || op.equals("cos") || op.equals("tan"); } public static void main(String[ ] args) { Scanner input=new Scanner (System .in); System .out.println ("Enter 1st num"); int a=input.nextInt(); System .out.println ("Enter operation"); String op=input.next(); if (isOneOperandOperation(op)) { // convert a from degrees to radians. double angleInRadians = a * Math.PI / 180; if (op.equals("sin")) System.out.println(Math.sin(angleInRadians)); else if (op.equals("cos")) System.out.println(Math.cos(angleInRadians)); else System.out.println(Math.tan(angleInRadians)); } else { System.out.println ("Enter 2stnum"); int b=input.nextInt(); if(op.equals("+")){ System.out.println(a+b); } else if(op.equals("-")){ System.out.println(a-b); } else if(op.equals("*")){ System.out.println(a*b); } else if (op.equals("/")){ System.out.println(a/b); } else if (op.equals("pow")){ System.out.println(Math.pow(a, b)); } } } }
6th Feb 2021, 9:26 PM
Josh Greig
Josh Greig - avatar
0
Dark Light please don't ask the same question multiple times https://www.sololearn.com/discuss/2688736/?ref=app
6th Feb 2021, 9:49 PM
Benjamin Jürgens
Benjamin Jürgens - avatar