2、学习单元测试和代码覆盖率工具的使用

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

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.fan.mju;
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		View v = new View();
	}
}
package cn.fan.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);
	}
}
(2)把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;
package cn.fan.mju;

import java.util.Scanner;

public class Main {
	public static void main (String[] args){
		Main a = new Main();
		a.View();
	}
	public void findWord(String str){
		String[] arrayWord =str.split(" ");
		for(int i=arrayWord.length-1;i>=0;i--){
			System.out.print(arrayWord[i]+" ");
		}
	}
	public void View(){
		Scanner input =new Scanner(System.in);
		System.out.println("请输入字符串,单词用空格隔开,回车结束:");
		String str=input.nextLine();
		Main a = new Main();
		a.findWord(str);
	}
}

转载于:https://www.cnblogs.com/weizhinumber/p/5316747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值