1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import java.util.HashMap; import java.util.Map; import java.util.Scanner;
public class NumberDemo { public static void main(String[] args) { String[] numberLetter = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; Map<String, Integer> maps = new HashMap(); for (int i = 0; i < numberLetter.length; i++) { maps.put(numberLetter[i], i); } Scanner input = new Scanner(System.in); System.out.println("请输入英文数字,用逗号分割!"); String str = input.nextLine(); String[] inputStr = str.split(","); try { showNumber(maps, inputStr); } catch (InputException e) { System.out.println(e.getExcepMessage()); } }
private static void showNumber(Map<String, Integer> maps, String[] inputStr) throws InputException { int count = 0; for (int i = 0; i < inputStr.length; i++) { inputStr[i] = inputStr[i].toLowerCase(); if (maps.get(inputStr[i]) != null) { count++; } } if (inputStr.length != count) { throw new InputException("输入异常!"); } for (int i = 0; i < inputStr.length; i++) { System.out.print(maps.get(inputStr[i])); } } }
class InputException extends Exception { private String message; public InputException(String message) { this.message = message; } public String getExcepMessage() { return message; } }
|