• 软件测试第二次作业1(1)(2)


    (1)实现代码如下:

    package cn.guo.mju;

     

    public class Action {

        public void findWord(String str){//划分单词

            String[] arrayWord =str.split(" ");

            Action a = new Action();

            a.frequency(arrayWord);

        }

        public void frequency(String[] arrayWord){

            String[] word = new String[arrayWord.length];//存放遍历过的单词

            int time[]=new int[arrayWord.length];//存放记录单词出现次数

            boolean t = true;

            for(int i=0;i<arrayWord.length;i++){

                for(int j=0;j<arrayWord.length;j++){//遍历已遍历过的单词表

                    if(arrayWord[i].equals(word[j])){

                        System.out.println("单词重复!!!");

                        t=false;//如果单词重复则跳过

                    }

                }

                if(t==true){//单词初始次数为1

                    word[i]=arrayWord[i];

                    time[i]=1;

                    for(int j=i+1;j<arrayWord.length;j++){//遍历字符串,记录次数

                        if(arrayWord[i].equals(arrayWord[j])){

      time[i]++;

                        }

                    }

                }

            }

            for(int i = 0;i<arrayWord.length;i++){//遍历输出次数

                if(word[i]!=null){

                    System.out.println("单词:"+word[i]+"出现了"+time[i]+"次。");

                }

            }

        }

             }

     

     

    package cn.guo.mju;

     

    import java.util.Scanner;

     

    public class View {

        public View(){

            Scanner input =new Scanner(System.in);

            System.out.println("请输入字符串,单词用空格隔开,回车结束:");

            String str=input.nextLine();

            Action a = new Action();

            a.findWord(str);

        }

    }

    package cn.guo.mju;

     

    public classMain {

         

        /**

         * @param args

         */

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            View v = new View();

        }

    }

    结果如下:

    (2)单元测试的实现代码如下:

    package cn.guo.mju;

     

     

    import org.junit.Before;

     

    import cn.gmh.mju.test2;

     

    public class ActionTest {

     

        @Before

        public void setUp() throws Exception {

        }

        public void test() throws Exception {

           String str="Hello World My First Unit Test";

           Action action=new Action();

           action.findWord(str);

           }

     

    }

    结果如下:

     

  • 相关阅读:
    【PS技巧】常用概念和功能操作
    【存储】RAID磁盘阵列选择
    【Python 01】Python一种面向对象、解释型计算机程序设计语言
    【PS技巧】如何校正倾斜的图片
    【阿里巴巴大数据实践笔记】第14章:存储和成本管理
    【阿里巴巴大数据实践笔记】第13章:计算管理
    【阿里巴巴大数据实践笔记】第9章:阿里巴巴数据整合及管理体系
    今晚直播丨抢鲜体验-openGauss入门
    详述一则数据库死锁故障的分析过程
    前端学习笔记(一)HTML入门
  • 原文地址:https://www.cnblogs.com/guomeihong/p/5325495.html
Copyright © 2020-2023  润新知