package method; /* * 四則運算計算器 * */ import java.util.Scanner; public class Demo07 { public static void main(String[] args) { Scanner scanner = new Scann ...
package method;
/*
* 四則運算計算器
* */
import java.util.Scanner;
public class Demo07
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int a = 0;
int num1 = 0, num2 = 0;
System.out.println("請選擇運行方法");
System.out.println("1.加法\t2.減法\t3.乘法\t4.除法");
if (scanner.hasNextInt())
{
a = scanner.nextInt();
}
switch (a)
{
case 1 ->
{
System.out.println("請輸入兩個操作數");
if (scanner.hasNextInt())
{
num1 = scanner.nextInt();
num2 = scanner.nextInt();
}
System.out.println(add(num1, num2));
}
case 2 ->
{
System.out.println("請輸入兩個操作數");
if (scanner.hasNextInt())
{
num1 = scanner.nextInt();
num2 = scanner.nextInt();
}
System.out.println(sub(num1, num2));
}
case 3 ->
{
System.out.println("請輸入兩個操作數");
if (scanner.hasNextInt())
{
num1 = scanner.nextInt();
num2 = scanner.nextInt();
}
System.out.println(mpc(num1, num2));
}
case 4 ->
{
System.out.println("請輸入兩個操作數");
if (scanner.hasNextInt())
{
num1 = scanner.nextInt();
num2 = scanner.nextInt();
}
System.out.println(div(num1, num2));
}
default -> System.out.println("輸入非法");
}
scanner.close();
}
//加法運算
public static int add(int num1, int num2)
{
return num1 + num2;
}
//減法運算
public static int sub(int num1, int num2)
{
return num1 - num2;
}
//乘法運算
public static int mpc(int num1, int num2)
{
return num1 * num2;
}
//除法運算
public static int div(int num1, int num2)
{
return num1 / num2;
}
}