请写一个java程序,要求如下:String x[]={"zero","one","two", "three","four", "five","six","seven","eight","nine"} ; String y[]={"ten","eleven","twelve","thirteen","fourteen","fifteen", "sixteen","seventeen","eighteen","nineteen" }; String z[]={"twenty","thirty","fourty","fifty", "sixty","seventy", "eighty","ninety" }; 设置参数一个小于100的英文,将其翻成数字示。例如输入fifty one,输出51
时间: 2023-06-29 14:16:51 浏览: 213
好的,以下是一个简单的 Java 程序,可以将小于 100 的英文翻译成数字:
```
import java.util.Scanner;
public class EnglishToNumber {
public static void main(String[] args) {
String[] x = {"zero","one","two", "three","four", "five","six","seven","eight","nine"} ;
String[] y = {"ten","eleven","twelve","thirteen","fourteen","fifteen", "sixteen","seventeen","eighteen","nineteen" };
String[] z = {"twenty","thirty","forty","fifty", "sixty","seventy", "eighty","ninety" };
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个小于100的英文单词或数字:");
String english = scanner.nextLine().trim().toLowerCase();
int number = 0;
if (english.matches("\\d+")) {
// 如果输入的是数字,直接将其转成整数
number = Integer.parseInt(english);
} else {
// 如果输入的是英文单词,按照题目要求进行翻译
String[] words = english.split(" ");
for (int i = 0; i < words.length; i++) {
String word = words[i];
int index = -1;
if (i == 0) {
// 处理十位数
index = indexOf(z, word);
if (index >= 0) {
number += (index + 2) * 10;
continue;
}
} else {
// 处理个位数
index = indexOf(x, word);
if (index >= 0) {
number += index;
continue;
}
index = indexOf(y, word);
if (index >= 0) {
number += index + 10;
continue;
}
}
System.out.println("无法识别的单词:" + word);
return;
}
}
System.out.println("翻译结果:" + number);
}
private static int indexOf(String[] array, String word) {
for (int i = 0; i < array.length; i++) {
if (array[i].equals(word)) {
return i;
}
}
return -1;
}
}
```
可以按照提示输入一个小于 100 的英文单词或数字,程序会将其翻译成数字并输出。例如输入 fifty one,输出 51。注意,程序对于无法识别的单词会直接退出。
阅读全文