import java.util.Scanner;
public class Main extends Hashmap{
public static void main(String[] args) {
Hashmap hm = new Hashmap();
int x=0;
Scanner input = new Scanner(System.in);
do{
System.out.print("Enter any integer value between 1 to 12: ");
x = input.nextInt();
}while(x<=0 || x>12);
Scanner sc = new Scanner(System.in);
//int number;
do {
while (!sc.hasNextInt())
{
System.out.println("That's not a number!");
sc.next();
}
x = sc.nextInt();
}while(x>=0);
String month = hm.getEntry(x);
System.out.println(month);
}
}
这里我需要限制用户输入字母。但它不起作用。 请帮忙......
答案 0 :(得分:0)
首先,再次查看polygenelubricants comprehensive answer您的问题。我很确定,他在例3中为你解决了这个问题。
然后,作为用户界面的替代设计:接受任何用户输入,验证输入,如果输入不正确,请提供有用的消息并要求用户再次输入值。这实现起来要容易得多,用户也应该对此感到满意。
答案 1 :(得分:0)
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<Integer, String>();
/**
* Populate hashmap.. Your logic goes here
*/
Scanner sc = new Scanner(System.in);
int x;
System.out.println("Enter a number between 1 and 12");
do {
while (!sc.hasNextInt()) {
System.out.println("That's not a number! Enter a number between 1 and 12");
sc.next();
}
x = sc.nextInt();
String month = hm.get(x);
System.out.println("The corresponding month is " + month);
} while (x >= 0);
}
我认为这是你想要实现的目标..