||
范例1: import java.io.*; class caculatortype { public void addmethod() { double d1,d2,d3; String s1,s2; BufferedReader mybuf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("please input the first number"); try { s1=mybuf.readLine(); d1=Double.parseDouble(s1); System.out.println("please input the second number"); s2=mybuf.readLine(); d2=Double.parseDouble(s2); d3=d1+d2; System.out.println(d1+"+"+d2+"="+d3); }catch(Exception e){} } public void submethod() { double d1,d2,d3; String s1,s2; BufferedReader mybuf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("please input the first number"); try { s1=mybuf.readLine(); d1=Double.parseDouble(s1); System.out.println("please input the second number"); s2=mybuf.readLine(); d2=Double.parseDouble(s2); d3=d1-d2; System.out.println(d1+"-"+d2+"="+d3); }catch(Exception e){} } public void mulmethod() { double d1,d2,d3; String s1,s2; BufferedReader mybuf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("please input the first number"); try { s1=mybuf.readLine(); d1=Double.parseDouble(s1); System.out.println("please input the second number"); s2=mybuf.readLine(); d2=Double.parseDouble(s2); d3=d1*d2; System.out.println(d1+"*"+d2+"="+d3); }catch(Exception e){} } public void didmethod() { double d1,d2,d3; String s1,s2; BufferedReader mybuf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("please input the first number"); try { s1=mybuf.readLine(); d1=Double.parseDouble(s1); System.out.println("please input the second number"); s2=mybuf.readLine(); d2=Double.parseDouble(s2); if(d2==0) { System.out.println("input error,the second number is 0"); } else { d3=d1/d2; System.out.println(d1+"/"+d2+"="+d3); } }catch(Exception e){} } } public class caculator extends caculatortype { public static void main(String args[]) throws IOException { char mychar; do { System.out.println("please selected your caculator type"); System.out.print("1.+\t"); System.out.print("2.-\t"); System.out.print("3./\t"); System.out.print("4.*\t"); System.out.println(); mychar=(char) System.in.read();System.in.skip(6); }while(mychar<'1'||mychar>'4'); System.out.println("\n"); caculator myc=new caculator(); switch(mychar) { case '1': myc.addmethod(); break; case '2': myc.submethod(); break; case '3': myc.mulmethod(); break; case '4': myc.didmethod(); break; default: System.out.println("is the end"); } } } 范例2: import java.io.*; abstract class Atype { protected double firstnumber,secondnumber,result; public void setfsnumber(double a,double b) { firstnumber=a; secondnumber=b; } abstract void show(); } class add extends Atype { public void show() { result=firstnumber+secondnumber; System.out.println(firstnumber+"+"+secondnumber+"="+result); } } class sub extends Atype { public void show() { result=firstnumber-secondnumber; System.out.println(firstnumber+"-"+secondnumber+"="+result); } } class mul extends Atype { public void show() { result=firstnumber*secondnumber; System.out.println(firstnumber+"*"+secondnumber+"="+result); } } class divide extends Atype { public void show() { if(secondnumber==0) { System.out.println("input error"); } else { result=firstnumber/secondnumber; System.out.println(firstnumber+"/"+secondnumber+"="+result); } ; } } public class abstractc { public static void main(String args[]) throws IOException { add myadd=new add(); sub mysub=new sub(); mul mymul=new mul(); divide mydivide=new divide(); BufferedReader mybuf=new BufferedReader(new InputStreamReader(System.in)); String mystring1,mystring2; double d1=0,d2=0; char type; do { System.out.println("this is a caculator example"); System.out.println("please selected caculator type 1.+ 2.- 3.* 4./"); type=(char) System.in.read(); System.in.skip(6); }while(type<'1'||type>'4'); /* do {*/ switch(type) { case '1': try { System.out.println("please input the first number"); mystring1=mybuf.readLine(); d1=Double.parseDouble(mystring1); System.out.println("please input the second number"); mystring2=mybuf.readLine(); d2=Double.parseDouble(mystring2); }catch(Exception e){} myadd.setfsnumber(d1,d2); myadd.show(); break; case '2': try { System.out.println("please input the first number"); mystring1=mybuf.readLine(); d1=Double.parseDouble(mystring1); System.out.println("please input the second number"); mystring2=mybuf.readLine(); d2=Double.parseDouble(mystring2); }catch(Exception e){} mysub.setfsnumber(d1,d2); mysub.show(); break; case '3': try { System.out.println("please input the first number"); mystring1=mybuf.readLine(); d1=Double.parseDouble(mystring1); System.out.println("please input the second number"); mystring2=mybuf.readLine(); d2=Double.parseDouble(mystring2); }catch(Exception e){} mymul.setfsnumber(d1,d2); mymul.show(); break; case '4': try { System.out.println("please input the first number"); mystring1=mybuf.readLine(); d1=Double.parseDouble(mystring1); System.out.println("please input the second number"); mystring2=mybuf.readLine(); d2=Double.parseDouble(mystring2); }catch(Exception e){} mydivide.setfsnumber(d1,d2); mydivide.show(); break; default: System.out.println("is the end"); } // } while(type<'1'||type>4); } } |
手机版|北京交通大学论坛-知行信息交流平台 ( BJTUICP备13011901号 )
GMT+8, 2023-6-2 16:14
Powered by Discuz! X3.4
Copyright © 2001-2020, Tencent Cloud.