我试图让用户输入字符串并让我的程序返回相应的数字。例如,如果用户键入“one”,则程序返回“1”,以此类推。我正在附上我到目前为止所构建的内容,但我被困住了,下班后我已经在这里待了两天 - 非常感谢任何建议!我知道最后一行是不正确的,但我希望我朝着正确的方向前进以完成这项工作。
import java.util.Hashtable;
import java.util.Scanner;
class HTDemo {
public static void main(String args[]) {
// creates a Hashtable <key, value>
Hashtable<String, Integer> numbers = new Hashtable<String, Integer>();
// adds values to the Hashtable
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));
numbers.put("four", new Integer(4));
numbers.put("five", new Integer(5));
numbers.put("six", new Integer(6));
numbers.put("seven", new Integer(7));
numbers.put("eight", new Integer(8));
numbers.put("nine", new Integer(9));
numbers.put("ten", new Integer(10));
Scanner sc = new Scanner(System.in);
System.out.print("Input a string value 'one' through 'ten': ");
String line = sc.nextLine();
System.out.println("You entered: " + line + numbers.containsKey(object));
}
}