作业5.1

package case1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class no1 {
    public static void main(String[] args) {
        int x,y;
        String z ,p,p1;
        Scanner inr = new Scanner(System.in);
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in)) ;
        
        System.out.print("请输入第一个数字:") ;
        while(true)
        {
            try
            {
                p=in.readLine();
                //如果转换异常则输入的不是数字
                x=Integer.parseInt(p);
                if(x>0)
                {
                    System.out.println("\n") ;
                }
                break;
            }
            catch(Exception e)
            {
                System.out.println("\n错误") ;
            }
        }
        
        System.out.print("请输入第二个数字:") ;
        while(true)
        {
            
            try
            {
                p1=in.readLine();
                //如果转换异常则输入的不是数字
                y=Integer.parseInt(p1);
                if(y>0)
                {
                    System.out.println("\n") ;
                }
                break;
            }
            catch(Exception e)
            {
                System.out.println("\n错误") ;
            }
        }
        
        System.out.println("请输入运算符(+,-,*,/):");
        z = inr.nextLine();    
        
        
        System.out.println("\n"+x+" "+z+" "+y+"=");
        no2 test = new no2();
        test.case1(z, x, y);
    }
}



package case1;

public class no2 {
            public void case1(String cc, double a, double b) {
                if (cc.equals("+")) {
                    System.out.println(a + b);
                } else if (cc.equals("-")) {
                    System.out.println(a - b);
                } else if (cc.equals("*")) {
                    System.out.println(a * b);
                } else if (cc.equals("/")) {
                    if (b != 0) {
                        System.out.println(a / b);
                    } else {
                        System.out.println("division by zero!");
                    }
                } else {
                    System.out.println("运算符不符合!");
                }
            }
    }

同伴:董大为  http://www.cnblogs.com/case1/

posted @ 2015-04-30 10:44  57叶鹏  阅读(113)  评论(0编辑  收藏  举报