在java中如何从控制台输入数字 请问一下高手,在java中如何从控制台输入数字?谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是网上的一个例子: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]); } } 如何确保调用super.XXX() java中图片处理问题 请问内联类应该怎么设置变量 Java实现文件的读取并初始化给数组 object landscapes and lifetimes 构建jsp环境的问题 如何建立一个非模式的Dialog窗口 初学者的困惑?在线等待!! 入门者 问 如何使用自己写的类 (详细见内) Java代码格式化者 2.0发布 支持注释,支持中文字符 socket如何实现变量传递? File 类问题!!
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]);
}
}