從控制臺接收用戶輸入的一行英文句子,把句子的最前面兩個單詞移到句子的最后面去,并整理句子的大小寫及標點符號,將新的句子輸出。
input: The dog ran through the gate and down the street.
output: Ran through the gate and down the street, the dog?
input: My coat was too heavy for this warm day!
output: Was too heavy for this warm day, my coat?
uj5u.com熱心網友回復:
找到第二個空格的index,取substring, 再把index之前的string 拿出來連接到后面,最后大寫第一個charuj5u.com熱心網友回復:
interface cope{public String cope(String target);
}
class newstr{
public String newstr(String target,cope s){
return s.cope(target);
}
}
class newlast implements cope{
private String[] str;
private String lastword="";
private int last=0;
private String oldlast="";
private String newlast="";
private String newstr="";
public String cope(String target){
String[] str= target.split("\\s+");
lastword=str[str.length-1];
last=lastword.length()-1;
oldlast=lastword.substring(last,last+1);
newlast=lastword.replace(oldlast,",");
for(int x=2;x<str.length;x++){
if(x==2){
String tem= str[x].substring(0, 1).toUpperCase()+str[x].substring(1);
newstr+=tem+" ";
}else if(x==str.length-1){
newstr+=newlast;
}else{
newstr+=str[x]+" ";
}
}
newstr=newstr+str[0].substring(0,1).toLowerCase()+str[0].substring(1)+" "+str[1]+"?";
return newstr;
}
}
class Test{
public static void main(String[] args){
String str1="The dog ran through the gate and down the street.";
String str2="My coat was too heavy for this warm day!";
String s1=new newstr().newstr(str1,new newlast());
String s2=new newstr().newstr(str2,new newlast());
System.out.println(s1);
System.out.println(s2);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/113241.html
標籤:Eclipse
