zoukankan      html  css  js  c++  java
  • java 限定控制台输入值的类型

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class 控制输入类型 {
    static InputStreamReader in;
    static BufferedReader reader;
    static
    {in=new InputStreamReader(System.in);
    reader=new BufferedReader(in);
    }


    //控制输入为String
    static String readString()
    {String s="";
    try
    { s=reader.readLine();

    }
    catch(IOException e)
    {System.out.println(e);
    System.exit(0);
    }
    return s;
    }

    //控制输入为char
    static char readChar()
    {char ch='a';
    try
    {
    String s=readString();
    ch=s.charAt(0);

    }
    catch(Exception e)
    {System.out.println("输入的数据类型不对,程序将退出");
    System.exit(0);
    }
    return ch;
    }

    //控制输入为int
    static int readInt()
    {String s=readString();
    int i=0;
    try{
    i=Integer.parseInt(s);
    }
    catch(Exception e)
    {System.out.println("输入的数据类型不对,程序将退出");
    System.exit(0);
    }
    return i;
    }

    //控制输入为double
    static double readDouble()
    {String s=readString();
    double d=0.0;
    try
    {d=Double.parseDouble(s);
    }
    catch(Exception e)
    {System.out.println("输入的数据类型不对,程序将退出");
    System.exit(0);
    }
    return d;
    }


    //控制输入为float
    static float readFloat()
    {
    String s=readString();
    float f=0.0f;
    try
    {
    f=Float.parseFloat(s);
    }
    catch(Exception e)
    { System.out.println("输入的数据类型不对,程序将退出");
    System.exit(0);
    }
    return f;
    }

    }

  • 相关阅读:
    MongoDB新存储引擎WiredTiger实现(事务篇)
    mongodb存储引擎WiredTiger
    WiredTiger 4.1.0 发布,MongoDB 存储引擎
    MongoDB存储引擎、索引 原
    MongoDB Wiredtiger存储引擎实现原理
    MongoDB 副本集
    MongoDB 聚合
    MongoDB 索引
    mongodb 开发规范
    MongoDB 基本操作 数据库、集合
  • 原文地址:https://www.cnblogs.com/lxoy/p/4903051.html
Copyright © 2011-2022 走看看