请写一个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的英文,将其翻成数字示。
时间: 2023-06-29 12:16:51 浏览: 95
以下是一个满足要求的Java程序:
```
import java.util.Scanner;
public class NumberTranslation {
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("Please enter an English number (less than 100): ");
String englishNum = scanner.nextLine();
int num = -1;
for (int i = 0; i < x.length; i++) {
if (englishNum.equals(x[i])) {
num = i;
break;
}
}
if (num == -1) {
for (int i = 0; i < y.length; i++) {
if (englishNum.equals(y[i])) {
num = i + 10;
break;
}
}
}
if (num == -1) {
for (int i = 0; i < z.length; i++) {
if (englishNum.equals(z[i])) {
num = (i + 2) * 10;
break;
}
}
}
if (num == -1) {
System.out.println("Invalid input!");
} else {
System.out.println("The number is: " + num);
}
}
}
```
程序的逻辑是先通过循环依次检查输入的英文数字在数组x、y和z中的位置,如果找到了就将它对应的数字赋值给变量num。如果都没找到,就说明输入的是无效的英文数字,输出提示信息。最后,如果num的值不是-1,就输出对应的数字。
阅读全文