中西文化差异搞笑视频:java键盘输入数字赋值给数组

来源:百度文库 编辑:神马品牌网 时间:2024/05/12 14:58:28
import java.io.*;
public class ax{
public static void main(String args[]) throws IOException{
int i,max=0;
int a[]=new int[5];
for(i=0;i<=a.length-1;i++)
{a=System.in.read();
System.in.skip(2);}
for(i=0;i<=a.length-1;i++)
System.out.println(+a);
}
}
这代码是输入5个数到数组里,然后将它输出.
我输入1,2,3,4,5它输出的结果怎么是49,50,51,52,53

楼上的回答正确,所以应该做如下的修改:
可能不是最好的方法,有好的欢迎往上发:
import java.io.*;
public class ax
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i,max=0;
int a[]=new int[5];
System.out.println("Input datas,use enter to seperate them:");
for(i=0;i<=a.length-1;i++)
{
a[i]= Integer.parseInt(br.readLine());
}
for(i=0;i<=a.length-1;i++)
System.out.println(a[i]);
}
}

因为你输入的1,2,3,4,5并不是int型的,read函数返还的数是该字符的ascii码,而1的ascii码为49,相应的,2为50,以此类推