• 实验二


    题目一:

    1. 写一个Java程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”);

    2. 编写单元测试进行测试;

    3. 用ElcEmma查看代码覆盖率,要求覆盖率达到100%。

    题目二:

    1.写一个Java程序,把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;

    2.编写单元测试进行测试;

    3.用ElcEmma查看代码覆盖率,要求覆盖率达到100%

    ————————————————————————————————————————————————————————————————————————

    package cn.li.yi;

    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.li.yi;

    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.li.yi;

    public class Main {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    View v = new View();
    }
    }

    package cn.li.yi;

    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);
    }
    }

    ———————————————————————————————————————————————————————————————————————————————

    结果:

  • 相关阅读:
    nginx 安装配置
    mysql分表
    冲刺day7
    冲刺day6
    冲刺day5
    冲刺day4
    冲刺day3
    冲刺day2
    冲刺day1
    撰写《需求规格说明书》的工作流程、组员分工和组员工作量比例
  • 原文地址:https://www.cnblogs.com/jianjiandandandeyihui/p/5335154.html
Copyright © 2020-2023  润新知