• 作业5.2


    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("
    ") ;
                    }
                    break;
                }
                catch(Exception e)
                {
                    System.out.println("
    错误") ;
                }
            }
            
            System.out.print("请输入第二个数字:") ;
            while(true)
            {
                
                try
                {
                    p1=in.readLine();
                    //如果转换异常则输入的不是数字
                    y=Integer.parseInt(p1);
                    if(y>0)
                    {
                        System.out.println("
    ") ;
                    }
                    break;
                }
                catch(Exception e)
                {
                    System.out.println("
    错误") ;
                }
            }
            
            System.out.println("请输入运算符(+,-,*,/):");
            z = inr.nextLine();    
            
            
            System.out.println("
    "+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) {
                     jia test1 = new jia();
                 if (cc.equals("+")) {
                     
                     
                     test1.jia1(a , b);
                        } 
                 else if (cc.equals("-")) {
                     
                     test1.jian1(a , b);
                     
                     }
                 else if (cc.equals("*")) {
            
                     test1.chen1(a , b);
                     
                         }
                 else if (cc.equals("/")) {
                     
                            if (b != 0) {
                                test1.chu1(a , b);
                                
                             } 
                            else {
                             System.out.println("division by zero!");
                             throw new ArithmeticException();
                        }
                     } 
                 else {
                         System.out.println("运算符不符合!");
                     }
                }
        }
    
    
    package case1;
    
    public class jia {
    
        /**
         * @param args
         */
         public static  double jia1( double a, double b) {
             double anw=0;
             anw=a+b;
             System.out.println(a +"+"+ b);
             System.out.println(anw);
            return anw;
         }
         
         
         public static  double jian1( double a, double b) {
             double anw=0;
             anw=a-b;
             System.out.println(a +"-"+ b);
             System.out.println(anw);
            return anw;
         }
         
         
         public static  double chen1( double a, double b) {
             double anw=0;
             anw=a*b;
             System.out.println(a +"*"+ b);
             System.out.println(anw);
            return anw;
         }
         
         
         public static  double chu1( double a, double b) {
             double anw=0;
             anw=a/b;
             System.out.println(a +"/"+ b);
             System.out.println(anw);
            return anw;
         }
    }
    
    
    package case1;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    public class jiaTest {
    
        @Test
        public void testJia1() {
            double w;
            jia we=new jia();
            w=jia.jia1(1,2);
        }
    
        @Test
        public void testJian1() {
            double w;
            jia we=new jia();
            w=jia.jian1(3,1);
        }
    
        @Test
        public void testChen1() {
            double w;
            jia we=new jia();
            w=jia.chen1(3,1);
        }
    
        @Test
        public void testChu1() {
            double w;
            jia we=new jia();
            w=jia.chu1(4,2);
        }
    
    }
    同伴: 董大为 http://www.cnblogs.com/case1/
  • 相关阅读:
    如何在 Windows 7 上安装 TeX Live 2018
    CF 964C Alternating Sum
    WF 18 A 想法
    CLion 使用笔记
    数理统计学复习
    ssh无密码访问设置(ssh-keygen 的详解)
    mysql开启远程连接的方法
    mysql命令大全
    Linux下创建仓库的软件包createrepo
    Linux 格式化分区 报错Could not stat --- No such file or directory 和 partprobe 命令
  • 原文地址:https://www.cnblogs.com/yeyepengpeng/p/4487395.html
Copyright © 2020-2023  润新知