题目二:
1.写一个Java程序,把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;
2.编写单元测试进行测试;
3.用ElcEmma查看代码覆盖率,要求覆盖率达到100%
代码:
package com.vv.test;
public class VV {
public static void main(String[] args){
String str ="how are you";
System.out.println(reverse(str));
}
public static String reverse(String str){
int position=0;
int i=0;
StringBuilder sb=new StringBuilder();
while(i<str.length()){
char temp=str.charAt(i);
if(temp==' '){
position=0;
sb.insert(position,temp);
}else{
sb.insert(position,temp);
position++;
}
i++;
}
return sb.toString();
}
}