importjava.util.Scanner;
publicclassTest{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
System.out.print("請輸入第一個數字:");
doublefirst=scanner.nextDouble();
System.out.print("請輸入運算子(+-*/%):");
charoperator=scanner.next().charAt(0);
System.out.print("請輸入第二個數字:");
doublesecond=scanner.nextDouble();
doubleresult=0D;
switch(operator){
case"+":
result=first+second;
break;
case"-":
case"*":
result=first*second;
case"/":
result=first/second;
case"%":
result=first%second;
default:
result=first-second;
}
System.out.println(first+""+operator+second+"="+result);
----------testing
請輸入第一個數字:10
請輸入運算子(+-*/%):/
請輸入第二個數字:2
10.0/2.0=5.0
importjava.util.Scanner;
publicclassTest{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
System.out.print("請輸入第一個數字:");
doublefirst=scanner.nextDouble();
System.out.print("請輸入運算子(+-*/%):");
charoperator=scanner.next().charAt(0);
System.out.print("請輸入第二個數字:");
doublesecond=scanner.nextDouble();
doubleresult=0D;
switch(operator){
case"+":
result=first+second;
break;
case"-":
result=first+second;
break;
case"*":
result=first*second;
break;
case"/":
result=first/second;
break;
case"%":
result=first%second;
break;
default:
result=first-second;
break;
}
System.out.println(first+""+operator+second+"="+result);
}
}
----------testing
請輸入第一個數字:10
請輸入運算子(+-*/%):/
請輸入第二個數字:2
10.0/2.0=5.0