当前位置: 技术问答>java相关
怎样从键盘读取数字
来源: 互联网 发布时间:2015-05-31
本文导语: 我想从键盘读取数字(eg. int型变量的值) 应怎样实现? | public String readConsole() throws Exception { // Create a buffered reader with System.in, then // read a line from it. BufferedRea...
我想从键盘读取数字(eg. int型变量的值)
应怎样实现?
|
public String readConsole() throws Exception
{
// Create a buffered reader with System.in, then
// read a line from it.
BufferedReader breader;
breader = new BufferedReader(new InputStreamReader(System.in));
return breader.readLine();
}
int i = Integer.parseInt(StringValue);
---------------
shmilu@sina.com
{
// Create a buffered reader with System.in, then
// read a line from it.
BufferedReader breader;
breader = new BufferedReader(new InputStreamReader(System.in));
return breader.readLine();
}
int i = Integer.parseInt(StringValue);
---------------
shmilu@sina.com
|
import java.io.*;
public class Test {
public static void main (String[] args) {
String alpha;
DataInputStream DI = new DataInputStream(System.in);
System.out.println("Please enter the number");
try {
alpha = DI.readLine();
int a = Integer.parseInt(alpha);
System.out.print("You enter is "+a);
}
catch(Exception e) {
System.out.print("You enter a bad Character");
}
}
}
public class Test {
public static void main (String[] args) {
String alpha;
DataInputStream DI = new DataInputStream(System.in);
System.out.println("Please enter the number");
try {
alpha = DI.readLine();
int a = Integer.parseInt(alpha);
System.out.print("You enter is "+a);
}
catch(Exception e) {
System.out.print("You enter a bad Character");
}
}
}
|
//How to read from standard input
import java.io.*;
public class read {
public static void main(String[] args) {
DataInputStream wjl_in = //新生成一个data输入流的实例wjl_in;
new DataInputStream(
new BufferedInputStream(System.in));/*BufferendInputStream();一个过滤输入流,为从源输入流中读入的字节保留一个缓
冲区。*/
String s;
try {
while((s = wjl_in.readLine()).length() != 0)
System.out.println(s);
// An empty line terminates the program
} catch(IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class read {
public static void main(String[] args) {
DataInputStream wjl_in = //新生成一个data输入流的实例wjl_in;
new DataInputStream(
new BufferedInputStream(System.in));/*BufferendInputStream();一个过滤输入流,为从源输入流中读入的字节保留一个缓
冲区。*/
String s;
try {
while((s = wjl_in.readLine()).length() != 0)
System.out.println(s);
// An empty line terminates the program
} catch(IOException e) {
e.printStackTrace();
}
}
}