• 课堂测试-单元测试(比较大小)


    源程序代码:

    package maxtest;
    import java.util.InputMismatchException;
    import java.util.Scanner;
    class Max1{
        int LargeGet(int List[],int length)          //获得最大值的函数体
        {
            int i,max=List[0];
            for(i=0;i<length;i++)
            {
                if(List[i]>max)
                {
                    max=List[i];
                }
            }
            return max;                            //返回最大值
        }
    }
    public class Max extends Max1{
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Max1 c=new Max1();                                       //创建对象
            for(;;)
            {
                System.out.println("input the sum number(int):");
                try                                                 //捕捉输入类型不为整型的错误
                {
                    Scanner in=new Scanner(System.in);
                    int length=in.nextInt();
                    if(length==0)                                    //如果输入长度为0则报错
                    {
                        System.out.println("Type wrong!");
                        continue;
                    }
                    int[] List=new int[length];                      //定义数组长度
                    System.out.println("input the numbers(int):");
                    for(int i=0;i<length;i++)                        
                    {            
                        List[i]=in.nextInt();                        //依次输入数组元素
                    }
                    System.out.println(c.LargeGet(List,length));
                }
                catch(InputMismatchException m)                      //处理异常
                {
                    System.out.println("输入类型错误!应输入整型.");    
                }
                Scanner in=new Scanner(System.in);
                System.out.println("Try again press any one,Quit press Q");
                String f=in.next();                                 //用户决定是否继续测试程序
                if(f=="Q")
                    break;
                else
                    continue;
            }
        }
    }

    测试结果截图:

    1、输入数字类型不为整型时

    2、输入正序的数组时:

    3、输入乱序数组时:

    4、输入数组含负数时:

    5、输入的数组全为负数时:

    6、输入的数值是倒序时:

  • 相关阅读:
    [ZJOI2006]书架
    [NOI2005]维护数列
    Python 最佳实践
    python中使用多继承
    python 抽象类、抽象方法的实现
    30个有关Python的小技巧
    一行 Python 实现并行化 -- 日常多线程操作的新思路
    python日志模块logging
    在Python中怎么表达True
    有趣的库:pipe(类似linux | 管道)库
  • 原文地址:https://www.cnblogs.com/love528/p/5302737.html
Copyright © 2020-2023  润新知