文章出處
文章列表
import java.util.*; public class jisuan { public static void main(String[] args) { System.out.println("運算符號位:+,-,*,/"); Scanner sc=new Scanner(System.in); System.out.println("請輸入第一個數字:"); String str=sc.nextLine(); System.out.println("請輸入第二數字:"); String str2=sc.nextLine(); System.out.println("請輸入運算符:"); String str3=sc.nextLine(); try{ double a=Double.parseDouble(str); double b=Double.parseDouble(str2); Core core=new Core(); core.fengzhuan(str3, a, b); }catch(NumberFormatException e){ System.out.println("輸入有誤,請重新輸入"); Core core= new Core(); System.out.println("請輸入第一個數字:"); String str4=sc.nextLine(); System.out.println("請輸入第二數字:"); String str5=sc.nextLine(); double a=Double.parseDouble(str4); double b=Double.parseDouble(str5); core.fengzhuan(str3, a, b); } finally{} } } class Core { Scanner g=new Scanner(System.in); public void fengzhuan(String op,double a,double b){ 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("/")){ if(b==0){ System.out.println("運算錯誤請重新輸入"); System.out.println("請輸入b的值:"); b=g.nextDouble(); System.out.println(a/b); } else if(b!=0){ System.out.println(a/b); } } else{ String str3; System.out.println("符號輸入有誤,請重新輸入"); str3=g.next(); jisuan2 ji=new jisuan2(); ji.huansuan(str3, a, b); } } } class jisuan2 extends jisuan{ Scanner sc=new Scanner(System.in); public void huansuan(String str3,double a,double b){ if(str3.equals("+")){ System.out.println(a+b); }else if(str3.equals("-")){ System.out.println(a-b); }else if(str3.equals("*")){ System.out.println(a*b); }else if(str3.equals("/")){ if(b==0){ System.out.println("運算錯誤請重新輸入"); System.out.println("請輸入b的值:"); b=sc.nextDouble(); System.out.println(a/b); } else if(b!=0){ System.out.println(a/b); } } } }
文章列表
全站熱搜