Solution 2: Using Switch-Case Statements Code: importjava.util.Scanner;publicclassSimpleCalculatorSwitchCase{public static void main(String[]args){//Create a ScannerobjectforinputScanner scanner=new Scanner(System.in);//Input first number System.out.print("Enter the first number: ");double num1=...
除法"); int method = sc.nextInt(); switch (method) { case 1: System.out.println("请输入第1个数:"); double a1 = sc.nextDouble(); System.out.println("请输入第2个数:"); double a2 = sc.nextDouble(); System.out.println("运算结果为:"+c.add(a1, a2)); break; case 2: System....
switch (oper) { case '+': res = num1 + num2; break; case'_': res = num2 - num1;//注意顺序 break; case '*': res = num1 * num2; break; case '/': res = num2 / num1; break; default: break; } return res; } }...
in); int num=sc.nextInt(); System.out.println("请输入两个数字:"); calculator.num1 = scan.nextDouble(); calculator.num2 = scan.nextDouble(); switch(num){ case 1: calculator.add(); break; case 2: calculator.sub(); break; case 3: calculator.mul(); break; case 4: calculator.div...
To create a calculator with Java Swings, try the following code − Example import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.event.ActionEvent; ...
switch (operator) { case '+': System.out.println("结果为:" + (num1 + num2)); break; case '-': System.out.println("结果为:" + (num1 - num2)); break; case '': System.out.println("结果为:" + (num1 num2)); break; ...
Scanner reader=newScanner(System.in);while((str=reader.nextLine())!=null) { k=str.charAt(1); x=Integer.parseInt(String.valueOf(str.charAt(0))); y=Integer.parseInt(String.valueOf(str.charAt(2)));switch(k) {case'+': System.out.printf("%d\n",x+y);break;case'-': ...
C# switch case statement example: Here, we are going to design a simple calculator using switch case statement in C#.
Java Program for CalculatorThis program will read two integer numbers and calculate the arithmetic operators, in this example we used switch case and if else statement. User will enter a choice after entering two numbers and based on user choice program will return the result....
We need to declare a value where our output value is stored. We'll use a double type in case the answer is not a whole number. Step 6: Build Our Switch Statement For the last portion of the process section, we must make a switch statement. The switch receives the operator variable an...