作业5.1

 1 package case1;
 2 import java.io.BufferedReader;
 3 import java.io.InputStreamReader;
 4 import java.util.Scanner;
 5 public class no1 {
 6     public static void main(String[] args) {
 7         int x,y;
 8         String z ,p,p1;
 9         Scanner inr = new Scanner(System.in);
10         BufferedReader in=new BufferedReader(new InputStreamReader(System.in)) ;
11         
12         System.out.print("请输入第一个数字:") ;
13         while(true)
14         {
15             try
16             {
17                 p=in.readLine();
18                 //如果转换异常则输入的不是数字
19                 x=Integer.parseInt(p);
20                 if(x>0)
21                 {
22                     System.out.println("\n") ;
23                 }
24                 break;
25             }
26             catch(Exception e)
27             {
28                 System.out.println("\n错误") ;
29             }
30         }
31         
32         System.out.print("请输入第二个数字:") ;
33         while(true)
34         {
35             
36             try
37             {
38                 p1=in.readLine();
39                 //如果转换异常则输入的不是数字
40                 y=Integer.parseInt(p1);
41                 if(y>0)
42                 {
43                     System.out.println("\n") ;
44                 }
45                 break;
46             }
47             catch(Exception e)
48             {
49                 System.out.println("\n错误") ;
50             }
51         }
52         
53         System.out.println("请输入运算符(+,-,*,/):");
54         z = inr.nextLine();    
55         
56         
57         System.out.println("\n"+x+" "+z+" "+y+"=");
58         no2 test = new no2();
59         test.case1(z, x, y);
60     }
61 }
62 
63 
64 
65 package case1;
66 
67 public class no2 {
68             public void case1(String cc, double a, double b) {
69                 if (cc.equals("+")) {
70                     System.out.println(a + b);
71                 } else if (cc.equals("-")) {
72                     System.out.println(a - b);
73                 } else if (cc.equals("*")) {
74                     System.out.println(a * b);
75                 } else if (cc.equals("/")) {
76                     if (b != 0) {
77                         System.out.println(a / b);
78                     } else {
79                         System.out.println("division by zero!");
80                     }
81                 } else {
82                     System.out.println("运算符不符合!");
83                 }
84             }
85     }
View Code

 运行截图:

 

 

 

同伴: 叶鹏 ----  http://home.cnblogs.com/u/yeyepengpeng/

 

posted on 2015-04-30 10:39  10-董大为  阅读(115)  评论(1编辑  收藏  举报

导航