yi er san si wu liu qi ba jiu 分别对应123456789(附成长简单版本)

public static String trunNumber(String data)
	{
	    char[] charArray = data.toCharArray();
	    StringBuffer sb = new StringBuffer();
	    for (int i = 0; i <= charArray.length; i++) {
	    	if(i<=charArray.length-3) {
	    		String three = String.valueOf(charArray[i])+String.valueOf(charArray[i+1])+String.valueOf(charArray[i+2]);
				switch (three) {
					case "san":
						sb.append('3');
						break;
					case "liu":
						sb.append('6');
						break;
					case "jiu":
						sb.append('9');
						break;
					default:
						break;
				}
	    	}
	    	if(i<=charArray.length-2) {
	    		String er = String.valueOf(charArray[i])+String.valueOf(charArray[i+1]);
				switch (er) {
					case "yi":
						sb.append('1');
						break;
					case "er":
						sb.append('2');
						break;
					case "si":
						sb.append('4');
						break;
					case "wu":
						sb.append('5');
						break;
					case "q":
						sb.append('7');;
						break;
					case "ba":
						sb.append('8');
						break;
					default:
						break;
				}
	    		}
		}
	    return sb.toString();
	}

第二版:

yi er san si wu liu qi ba jiu 分别对应123456789(附成长简单版本)_第1张图片

	public static void getNum(String input){
		String[] pinyins = {"yi","er","san","si","wu","liu","qi","ba","jiu"};
		for (int i = 0; i < pinyins.length; i++) {
			String pinyin = pinyins[i];
			input = input.replace(pinyin, i+1 + "");
		}
		
		System.out.println(input);
		
	}

 

你可能感兴趣的:(java,机试题)