wordrevertion.java

来自「用java完成的查字典程序」· Java 代码 · 共 68 行

JAVA
68
字号
import java.util.ArrayList;
import java.io.*;
public class WordRevertion{
	public WordRevertion(){
	}
	public ArrayList<String> reverse(String origin){
		int length=origin.length();
		String prototype="";
		ArrayList<String> resultList=new ArrayList();
		resultList.add(origin);
		if(origin.endsWith("s")){
			if(origin.endsWith("ies")){
				prototype=origin.substring(0,length-3)+"y";
				resultList.add(prototype);
			}
			if(origin.endsWith("es")){
				    prototype=origin.substring(0,length-2);
			       	resultList.add(prototype);
			}
			prototype=origin.substring(0,length-1);
			resultList.add(prototype);

		}
		
		if(origin.endsWith("ing")){
			if(origin.charAt(length-4)==origin.charAt(length-5)){
				prototype=origin.substring(0,length-4);
				resultList.add(prototype);
			}
			resultList.add(origin.substring(0,length-2)+"e");
		    resultList.add(origin.substring(0,length-3)+"e");
			resultList.add(origin.substring(0,length-3));
		}
		
		if(origin.endsWith("ed")){
			if(origin.charAt(length-3)==origin.charAt(length-4)){
				prototype=origin.substring(0,length-3);
				resultList.add(prototype);
			}
			if(origin.endsWith("ied")){
				prototype=origin.substring(0,length-3)+"y";
			    resultList.add(prototype);
			}
			resultList.add(origin.substring(0,length-2)+"e");
			resultList.add(origin.substring(0,length-2));
		}
		
		return resultList;				
	}
	public static void main(String[]args){
		WordRevertion wr=new WordRevertion();
		try{
			while(true){
				BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
				String originWord=br.readLine();
				ArrayList resultList=wr.reverse(originWord.trim());
				for(int i=0;i<resultList.size();i++){
					System.out.print(resultList.get(i)+"  ");				
				}
				System.out.println();
				continue;
			}
			
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?