这种方式输入的是字符,我现在需要直接输入数字。
byte buf[]=new byte[10];
try{
System.in.read(buf);
}catch(Exception e){
System.out.println("catch error");
}
byte buf[]=new byte[10];
try{
System.in.read(buf);
}catch(Exception e){
System.out.println("catch error");
}
调试欢乐多
int aaa;
DataInputStream keybord=new DataInputStream(System.in);
try{
aaa=keybord.readInt();
}
catch(IOException e){}
public static void main(String[] args) {
int a;
if (args.length > 0)
a = Integer.parseInt(args[0]);
}
}在命令行中输入数值后,可以在程序中得到
import java.io.*;public class Jtest
{
public static void main (String args[])
{
int aaa=0;
System.out.print("Input a num:");
DataInputStream keybord=new DataInputStream(System.in);
try{
aaa=keybord.readInt();
}catch(IOException e){}
System.out.println("The num is:"+aaa);
}
}
运行结果:
Input a num:12
The num is:825363722这是什么值?搞不定!
{
public static void main (String args[])
{
System.out.print("Input a num:");
int choice;
byte buf[]=new byte[10];
try{
System.in.read(buf);
}catch(Exception e){
System.out.println("catch error");
}
String str=new String(buf);
//System.out.println("str=" + str.trim());
choice=Integer.parseInt(str.trim());
System.out.println("The num is:"+choice);
}
}
运行结果:
Input a num:1564798
The num is:1564798
out.flush()是把输出缓冲区的内容全部写入目标,在这里怎么用呢?
网络上的byte array可以用DataInputStream
如果是命令行的话,用Integer.parseInt(String)不就可以吗?