2012-09-19 157 views
2

我试图让我的用户输入不通过限制哪些用户可以输入崩溃我的程序,如:用户输入检查INT仅

  1. 仅是一个int
  2. 是1-30
  3. 之间

我写的代码只能工作到某个特定的点。如果你输入的不是int,它会检查它并要求你再次输入。然后再一次,如果你继续输入任何东西,但一个int我有另一个while循环,如果它键入一个int,并且如果它在1-30区外,那么它会要求用户再次输入。但是在此之后,如果用户输入另一个“除int外的任何东西”,程序将崩溃。我试图结合sc.hasnextint()和1-30条件之间的输入检查,但如果我把sc.nextint()之前sc.hasnextint()和用户输入任何东西,但一个int,程序崩溃。如果我把它放在condtion循环之后,那么用户输入将不会被声明。

int choose; 
System.out.print("type an integer: "); 
Scanner sc=new Scanner(System.in); 

while (!sc.hasNextInt()) { 
    System.out.println("only integers!: "); 
    sc.next(); // discard 
} 

choose=sc.nextInt(); 

while (choose<=0 || choose>30) 
{ 
    System.out.print("no, 1-30: "); 
    choose=sc.nextInt(); 
} 
sc.close(); 
+1

“*该程序会崩溃*”在什么意义上它会崩溃?抛出异常,程序进入无尽的while循环,还是完全发生其他事情? – Vulcan

+0

异常在线程 “主” java.util.InputMismatchException \t在java.util.Scanner.throwFor(未知来源) \t在java.util.Scanner.next(未知来源) \t在java.util.Scanner.nextInt (未知源) \t at java.util.Scanner.nextInt(Unknown Source) \t at Main.main(Main.java:21) – BubbleTree

+0

我在我的程序中这样做,它不会工作,所以我问如果有人知道一种不同的编码方式来制作我想要的作品。例如: 输入一个整数:gfgh only整数!:43434 不,1-30:ffgg * crash * – BubbleTree

回答

4

您需要的两个循环结合起来,这样既检查发生的每一次最终用户进入一些新的东西:

for(;;) { 
    if(!sc.hasNextInt()) { 
     System.out.println("only integers!: "); 
     sc.next(); // discard 
     continue; 
    } 
    choose=sc.nextInt(); 
    if(choose<=0 || choose>30) 
    { 
     System.out.print("no, 1-30: "); 
     continue; 
    } 
    break; 
} 

退出循环后,choose130之间的数字。 , 包括的。

+0

+1有一个永远为true的for循环并且在其中有独立的if条件。 – asgs

+0

根据OP在评论中提供的新信息,我会说这个答案不能解决问题,因为'sc.nextInt()'无法解析非整数输入。 – Vulcan

+0

@Vulcan没关系,因为不像OP的代码,除非'hasNextInt'返回'true',否则这个函数不会调用'nextInt'。 – dasblinkenlight

0
do: 
get number from user 
if non integer format is entered{ 
number = -1;} 
while: 1 < number < 30 
0
String choose = ""; 
    System.out.println("Test if input is an integer. Type 'quit' to exit."); 
    System.out.print("Type an integer: "); 
    Scanner sc=new Scanner(System.in); 

    choose = sc.nextLine(); 

    while (!(choose.equalsIgnoreCase("quit"))) { 
     int d = 0; 
     try { 
      d = Integer.parseInt(choose); 

      if (!(d > 0 && d < 31)) { 
       System.out.println("Being between 1-30"); 
      } else { 
       System.out.println("Input is an integer."); 
      } 
     } catch (NumberFormatException nfe) { 
      System.out.println("Enter only int"); 
     } 

     System.out.print("Type an integer to test again or 'quit' to exit: "); 
     sc = new Scanner(System.in); 
     choose = sc.nextLine(); 
    } 

    sc.close(); 
    System.out.print("Program ends."); 
0

见dasblinkenlight的awnser与NumberFormatException的渔获物。我正在考虑这样做。这也适用太:

您需要的两个循环结合起来,像这样:

while(true) { 
if(!sc.hasNextInt) { 
System.out.println("Only Integers!"); 
continue; 
} 
choose = sc.nextInt(); 
if(choose <= 0) { 
System.out.println("The number you entered was too small."); 
continue; 
} else if(choose > 30) { 
System.out.println("The number you entered was too large.\nMax: 30"); 
continue; 
} 
break; 
} 
sc.close();