zoukankan      html  css  js  c++  java
  • 自己做的demo---关于java控制台输入跟类型转化跟处理异常的demo

      1 package exception;
      2 
      3 import java.util.InputMismatchException;
      4 import java.util.Scanner;
      5 
      6 /*public class HelloA{
      7     public static void main(String args[])
      8     {
      9         Scanner in=new Scanner(System.in);
     10         System.out.println("输入一个数字");
     11         int a=in.nextInt();
     12         System.out.println("输入一个double类型");
     13         double b=in.nextDouble();
     14         System.out.println("输入一个string类型");
     15         String c=in.next();
     16         System.out.println("输入一个float类型");
     17         float d=in.nextFloat();
     18         System.out.println(a+"	"+b+"	"+c+"	"+d+"	");
     19     }
     20 }*/
     21 //string-->double
     22 /*public class HelloA{
     23     public static void main(String agrs[])
     24     {
     25         while(true)//实现多次输入,跟c中 while(scanf("%d",&a)!=EOF)一样
     26         {
     27             try{
     28                 Scanner in=new Scanner (System.in);
     29                 System.out.println("请输入一个String类型");
     30                 String a=in.next(); //如果输入"12.12",下面就可以运行,但是输入a这个字符就出现异常了
     31                 //String-->double
     32                 double a1=Double.parseDouble(a);
     33                 //System.out.println("String-->double	"+a+"	"+a1);
     34                 System.out.println("string-->double:"+a1);
     35         
     36                     
     37                 
     38                 
     39             }catch(NumberFormatException a){
     40                 System.out.println("请输入数字字符串/请输入");
     41                 //a.getMessage().contains("123);
     42             }finally{
     43                 System.out.println("这是一个Finally");   //顺便试了一下try catch finally
     44             }
     45             
     46         }        
     47     }
     48 }*/
     49 //String--->float
     50 /*public class HelloA{
     51     public static void main(String agrs[])
     52     {
     53         while(true)//实现多次输入,跟c中 while(scanf("%d",&a)!=EOF)一样
     54         {
     55             try{
     56                 Scanner in=new Scanner (System.in);
     57                 System.out.println("请输入一个String类型");
     58                 String a=in.next(); //如果输入"12.12",下面就可以运行,但是输入a这个字符就出现异常了
     59             
     60                 float a2=Float.parseFloat(a);
     61                 System.out.println("String-->float:"+a2);
     62             
     63                     
     64                 
     65                 
     66             }catch(NumberFormatException a){
     67                 System.out.println("请输入数字字符串/请输入");
     68                 //a.getMessage().contains("123);
     69             }finally{
     70                 System.out.println("这是一个Finally");   //顺便试了一下try catch finally
     71             }
     72             
     73         }        
     74     }
     75 }*/
     76 //String--->int
     77 /*public class HelloA{
     78     public static void main(String agrs[])
     79     {
     80         while(true)//实现多次输入,跟c中 while(scanf("%d",&a)!=EOF)一样
     81         {
     82             try{
     83                 Scanner in=new Scanner (System.in);
     84                 System.out.println("请输入一个String类型");
     85                 String a=in.next(); //如果输入"12.12",下面就可以运行,但是输入a这个字符就出现异常了
     86                 
     87                 int a3=Integer.parseInt(a);
     88                 System.out.println("String-->int:"+a3);//string转为int的时候要求String没有小数点的才可以
     89                     
     90                 
     91                 
     92             }catch(NumberFormatException a){
     93                 System.out.println("请输入数字字符串,同时不带小数点/请输入");
     94                 //a.getMessage().contains("123);
     95             }finally{
     96                 System.out.println("这是一个Finally");   //顺便试了一下try catch finally
     97             }
     98             
     99         }        
    100     }
    101 }*/
    102 //接受一个double,float,int,boolean类型
    103 public class HelloA{
    104     public static void main(String agrs[])
    105     {
    106         Scanner in=new Scanner(System.in);
    107         while(true)
    108         {
    109             try{
    110                 System.out.println("输入一个整形");
    111                 int a=in.nextInt();
    112                 System.out.println("输入一个double");
    113                 double b=in.nextDouble();
    114                 System.out.println("输入一个float");
    115                 float c=in.nextFloat();
    116                 System.out.println("输入一个boolean");
    117                 boolean d=in.nextBoolean();//这句肯定是会报错。如果不是输入true或则false
    118                 System.out.println(a+"	"+b+"	"+c+"	"+d);
    119             }catch(Exception e){
    120                 e.printStackTrace();
    121             }finally{
    122                 System.out.println("我来看热闹的,我是finally");
    123             }
    124             
    125             
    126         }
    127     }
    128 }
     1 Scanner in=new Scanner(System.in);
     2 
     3 in.nextInt();
     4 
     5 in.nextDouble();
     6 
     7 in.nextFloat();
     8 
     9 in.nextBoolean();
    10 
    11 12 
    13 in.next(); //字符串
    14 
    15 //string向各钟类型转化
    16 
    17 String a="12.12";
    18 
    19 String a1="12";
    20 
    21 Double b=Double.parseDouble(a);
    22 float b1=Float.parseFloat(a);
    23 
    24 int b2=Integer.parseInt(a1)
    25 //try catch finally 
    ---- 动动手指关注我!或许下次你又能在我这里找到你需要的答案!ZZZZW与你一起学习,一起进步!
  • 相关阅读:
    nginx.conf文件
    本地apache 可以正常访问,lnmp服务器访问404错误
    PHP网站从Apache转移到Nginx后产生404错误的原因和解决办法
    CGI,FAST-CGI,PHP-FPM的区别
    Lnmp修改php.ini配置
    APACHE服务器出现No input file specified.的完美解决方案
    EXCEL常用函数详解
    chrome extensions
    C++使用OLE高速读写EXCEL的源码
    C++读写EXCEL文件OLE,java读写excel文件POI 对比
  • 原文地址:https://www.cnblogs.com/zzzzw/p/4966864.html
Copyright © 2011-2022 走看看