1. 程式人生 > >java四則運算寫法

java四則運算寫法

java ati clas ase 一個數 args 第一個 ava 數字

public class 四則運算{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("請輸入第一個數字:");
int a = sc.nextInt();
System.out.print("請輸入運算字符:");
String str = sc.next();
char ch = str.charAt(0);
System.out.print("請輸入第二個數字:");
int b = sc.nextInt();
switch(ch)
{
case ‘+‘:
System.out.println(a+"+"+ b + "="+(a+b));
break;
case ‘-‘:
System.out.println(a+"-"+ b+ "="+(a-b));
break;
case ‘*‘:
System.out.println(a+"*"+ b+ "="+(a*b));
break;
case ‘/‘:
if(b==0){
System.out.println("被除數為零,運算無意義!");
break;
}
else {
System.out.println(a+"/"+ b+ " = "+(a/b));
break;
}
default:
System.out.println("運算符是無意義字符!");
}
}

java四則運算寫法