java吧 关注:1,295,065贴子:12,828,846
  • 4回复贴,共1

关于异常处理的

取消只看楼主收藏回复

//代码
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("请输入数字:520");
try{
int a=in.nextInt();
if(a==520){
System.out.println("输入正确!");
}
}catch(Exception e){
System.out.println("你输入有误,程序将在3秒后退出");
System.exit(0);
}
}
}
/*怎么把System.exit(0);换成定时退出啊,或者将程序跳转到 int a=in.nextInt();*/


IP属地:江西来自Android客户端1楼2015-01-18 00:00回复
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args) throws InterruptedException
    {
    boolean b = false;
    Scanner in=new Scanner(System.in);
    System.out.println("请输入数字:520");
    try{
    int a=in.nextInt();
    if(a==520){
    System.out.println("输入正确!");
    }else{
    b=true;
    }
    }
    catch (Exception e)
    {
    System.out.println("你输入有误,程序将在3秒后退出");
    }finally{
    if(b){
    System.out.println("你输入有误,程序将在3秒后退出");
    try{
    Thread.currentThread().sleep(3000);
    }catch(InterruptedException e){
    }
    finally{
    System.exit(0);
    }
    }
    }
    }
    }
    /*我写了,貌似还是没有退出啊*/


    IP属地:江西来自Android客户端6楼2015-01-18 01:23
    回复
      2026-01-18 22:48:17
      广告
      不感兴趣
      开通SVIP免广告
      好了,有用啊,刚才写错b的值了


      IP属地:江西来自Android客户端7楼2015-01-18 01:24
      回复
        /*我改写了一下,也有用啊*/
        import java.util.Scanner;
        public class Main
        {
        public static void main(String[] args) throws InterruptedException
        {
        Scanner in=new Scanner(System.in);
        System.out.println("请输入数字:520");
        try{
        int a=in.nextInt();
        if(a==520){
        System.out.println("输入正确!");
        }
        }
        catch (Exception e)
        {
        System.out.println("你输入有误,程序将在3秒后退出");
        Thread.currentThread().sleep(3000);
        }
        finally{
        System.exit(0);
        }
        }
        }


        IP属地:江西来自Android客户端8楼2015-01-18 01:29
        收起回复
          /*我在修改了一下,最终结果是这样的*/
          import java.util.Scanner;
          public class Main
          {
          public static void main(String[] args) throws InterruptedException
          {
          Scanner in=new Scanner(System.in);
          System.out.println("请输入数字:520");
          try{
          int a=in.nextInt();
          if(a==520){
          System.out.println("输入正确!");
          }else{
          System.out.println("你输错了,程序将在3秒后退出");
          Thread.currentThread().sleep(3000);
          System.exit(0);
          }
          }
          catch (Exception e)
          {
          System.out.println("你输入有误,程序将在3秒后退出");
          Thread.currentThread().sleep(3000);
          System.exit(0);
          }
          }
          }


          IP属地:江西来自Android客户端9楼2015-01-18 10:18
          回复