|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
java初学者,一定对从键盘输入数据感到困难,使用下面的类Input,可以 方便的从键盘输入数据: 使用方式举例: String s=Input.readString(); 读入字符串 int i=Input.readInt(); 读入整数 下面是java输入输出基本类Input类的源代码: import java.io.*; class Input {static InputStreamReader in; static BufferedReader reader; static {in=new InputStreamReader(System.in); reader=new BufferedReader(in); } static String readString() {String s=""; try { s=reader.readLine(); } catch(IOException e) {System.out.println(e); System.exit(0); } return s; } 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; } 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; } 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; } 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; } } 用法举例,从键盘输入十个整数: class InoutData {public static void main(String args[]) { int a[]=new int[10]; for(int i=0;i<10;i++) { System.out.println("请输入数"); a[i]=Input.readInt(); } for(int i=0;i<10;i++) System.out.println("a["+i+"]="+a[i]); } } 返回类别: 教程 上一教程: 一些异常有用的JAVA常用方式(1) 下一教程: 漫谈Java程序设计中的接口应用 您可以阅读与"Java-输入基本类"相关的教程: · java的一些基本概念 · Java入门需把握的30个基本概念 · Java程序设计的基本结构 · Java程序设计的基本结构 · [Java]基本的学习实例 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |