2010-06-18 61 views
-1
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); 




} 
    } 

这里我需要限制用户输入英文字母。但它不工作。 请帮忙...我已经申请了一个不允许使用字母表的检查。但它不能正常工作

+2

我不太有答案给你,而且一目了然...... 1.你为什么要扩展HashMap的?看起来你不需要那样做。 2.你的变量“hm”没有填充任何值,所以你得到“月”的最后两行不起作用。 – 2010-06-18 05:38:54

+0

程序工作正常。只是检查不起作用。 – 2010-06-18 05:54:04

回答

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); 

} 

我觉得这是你想达到什么..

+0

谢谢,但它仍然没有按我想要的方式工作。 – 2010-06-18 08:43:52

+1

请具体说明。你想如何表现?准确地陈述问题描述。 – chedine 2010-06-18 09:09:57

+0

谢谢 我知道它有一些变化。 – 2010-06-18 09:17:05

0

首先,看看你的问题polygenelubricants comprehensive answer。我很确定,他在示例3中为你解决了它。

然后,作为用户界面的替代设计:接受任何用户输入,验证输入,如果输入不正确,请提供有用的消息并要求用户再次输入一个值。这很容易实现,用户也应该对此感到满意。

+0

问题尚未解决。 – 2010-06-18 06:36:18