java吧 关注:1,295,049贴子:12,828,834
  • 23回复贴,共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回复
    main.sleep(3*10^9,0);
    好像是这样


    IP属地:湖北2楼2015-01-18 00:06
    收起回复
      2026-01-18 19:07:49
      广告
      不感兴趣
      开通SVIP免广告
      我知道了,用这个:
      Thread.currentThread().sleep(3000);
      前面的方法返回当前运行的线程的实例
      然后调用sleep方法
      你最后说的返回输入用递归就可以。


      IP属地:湖北3楼2015-01-18 00:31
      收起回复


        IP属地:湖北4楼2015-01-18 01:13
        收起回复


          IP属地:湖北5楼2015-01-18 01:17
          收起回复
            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
            回复
              好了,有用啊,刚才写错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
                收起回复
                  2026-01-18 19:01:49
                  广告
                  不感兴趣
                  开通SVIP免广告
                  /*我在修改了一下,最终结果是这样的*/
                  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
                  回复