2014-09-10 70 views
-1

我做了这个Java代码来输入一个数字:防止输入错误

public static void main(String[] args) 
{ 
    int temp; 

    do{ 

     Scanner scan = new Scanner(in); 
     out.print("enter number "); 
     temp = scan.nextInt(); 




     if(temp >= 5 && temp <= 40){ 
      int x = (temp-1)*2 +1; 
      int y = x/2; 
      int z = 1; 
      for(int i=0; i<temp-1; i++) 
      { 
       for(int j=0; j<=y; j++) 
       { 
        out.print(" "); 
       } 
       for(int k = 0; k<z; k++) 
       { 
        out.print("|"); 
       } 
       out.println(); 
       y--; 
       z+=2; 
      } 

      for(int c = 0; c < 1 + temp/10; c++) { 
       for (int i = 0; i <= x/2; i++) 
       { 
        System.out.print(" "); 
       } 
       System.out.println("|"); 
      } 
     }else{ 
      out.print("enter a number between 5 and 40"); 
     } 
    }while(temp != 0); 
} 

}

然而,这要是我,例如输入字母或无效字符返回一个错误。我想知道如何在不使程序崩溃的情况下显示错误消息,然后再次询问问题,直到输入正确为止?

回答

1

import java.io. *;

import java.util。*;

公共类主要

{

public static void main(String[] args) 
{ 

    int temp=0; 
    boolean error=false; 
    do{ 
     error=false; 
     try 
     { 
      Scanner scan = new Scanner(System.in); 
      System.out.print("enter number "); 
      temp = scan.nextInt(); 
      if(temp==0) 
       break; 



      if(temp >= 5 && temp <= 40) 
      { 
       int x = (temp-1)*2 +1; 
       int y = x/2; 
       int z = 1; 
       for(int i=0; i<temp-1; i++) 
       { 
        for(int j=0; j<=y; j++) 
        { 
         System.out.print(" "); 
        } 
        for(int k = 0; k<z; k++) 
        { 
         System.out.print("|"); 
        } 
        System.out.println(); 
        y--; 
        z+=2; 
       } 

       for(int c = 0; c < 1 + temp/10; c++) 
       { 
        for (int i = 0; i <= x/2; i++) 
        { 
         System.out.print(" "); 
        } 
        System.out.println("|"); 
       } 
      } 
      else 
      { 
       System.out.println("enter a number between 5 and 40"); 
      } 
     }catch(Exception e) 
     { 
      System.out.println("Enter a valid number..try again"); 
      error=true; 
     } 
    }while(temp != 0 || error); 
} 

}

当错误或异常的scan.nextInt(即发生)的异常被抛出,当你还没有抓到异常的JVM停止执行程序。

因此,总是编写可以在try {}块内引发异常的语句,并立即跟随catch(Exception e){}块来捕获异常。如果没有发生异常,catch块将不会执行。如果在try {}块内发生任何错误:控件跳转到catch块并且它被执行,并且try {}中的所有其他语句(在错误行之后)被忽略。

尝试 { .. 错误 .. //跳过 .. } 赶上(例外五){ ... ... //处理异常 } // 来控制这里执行catch块

+0

你的答案需要一些解释 – 2014-09-10 22:51:47

+0

据我所知,在主要方法中抛出异常并不是很好的做法 – 2014-09-10 22:52:29

+0

@KickButtowski ..他没有抛出异常。他正在赶上它 – RKodakandla 2014-09-10 22:54:57

0

以字符串形式读取用户的值,然后尝试将其解析为try {} catch {}块中的数字,如果引发异常解析,请告诉用户只有数字是可接受的。如果没有继续处理他们给你的号码。

+0

我更直观;你能告诉我一个例子或使用我的代码吗?谢谢 – 2014-09-10 22:34:54

+1

@Johnny_b:Google是你的朋友。有成千上万的例子。只需查找try/catch字符串,或类似的东西。 – 2014-09-10 22:38:25

+0

把“Shouvik Roy”的代码放在一个循环中,直到你得到一个有效的输入并且你很好。 – Dave 2014-09-10 22:42:43

0

从@Shouvik罗伊的回答后扩展..

import java.util.Scanner; 

public class Bingo { 

    public static void main(String[] args){ 
     boolean isInt = false; 
     int temp; 
     System.out.print("enter a number "); 
     do{ 
      Scanner scan = new Scanner(System.in); 

      try 
      { 
       temp = scan.nextInt(); 
       isInt = true; 
      } 
      catch(Exception e) 
      { 
       System.out.println("Couldn't read number. Reason - "+e.getMessage()); 
       System.out.println("Enter a valid number "); 
      } 

     }while(!isInt); 

    } 
} 

------------------编辑-------------------

boolean isInt = false; 
System.out.print("enter a number "); 


do{ 
    try 
    { 
     Scanner scan = new Scanner(in); 
     temp = scan.nextInt(); 
     isInt = true; 
    } 
    catch(Exception e) 
    { 
     System.out.println("Couldn't read number. Reason - "+e.getMessage()); 
     System.out.println("Enter a valid number "); 
    } 
}while(!isInt) 
+0

如果你的扫描仪遇到问题,你将如何处理它?你以为没有解释给出答案是正确的和有帮助的? – 2014-09-10 22:54:03

+0

@KickButtowski在控制台中添加了打印错误信息的行.. – RKodakandla 2014-09-10 22:57:19

+0

谢谢,但是你能否把它放到我的代码中去看看你到底是什么意思?我有一些if和while语句,所以我不知道你的方法是否与我的代码 – 2014-09-10 23:09:04

0

Scanner有一个方法来检查下一个标记是否是整数:hasNextInt

你可以这样做:

package com.sandbox; 

import java.util.Scanner; 

public class Sandbox { 

    public static void main(String[] args) { 
     Scanner scanner = new Scanner(System.in); 

     System.out.println("Enter an integer:"); 
     while (!scanner.hasNextInt()) { 
      System.err.println("Wrong! Enter an integer"); 
      scanner.next(); 
     } 
     System.out.println("Your integer was: " + scanner.nextInt()); 
    } 


} 

此担任你想要在Windows控制台上。我有点担心它会在Linux/Mac上错误地工作。任何人都在为我测试它吗?如果这样做,我最喜欢它,因为没有尝试/抓住。

+0

只是一个问题,如果你的扫描仪碰到问题你将如何处理它? – 2014-09-10 22:56:50

+0

@KickButtowski它会遇到什么问题? – 2014-09-10 22:57:08

+0

先生,我相信你很聪明,已经知道它已经 – 2014-09-10 22:58:06