public class A
{
public static void main(String[] args)
{
String s;
int d;
s=args[0];
d=Interger.parseInt(s); //出错
System.out.println(d); //直接输出的d应该是整数哦。
}
}
{
public static void main(String[] args)
{
String s;
int d;
s=args[0];
d=Interger.parseInt(s); //出错
System.out.println(d); //直接输出的d应该是整数哦。
}
}
{
public static void main(String args[])
{
int d=Integer.parseInt(args[0]);
System.out.println(d);
}
}改成这样,能编译通过。但是运行后出错。
java A a
出错
java A Hello
也出错
java A 123后面是数字才不出错。a的话,我认为应该输出97.但是它却出错。谢谢高手修改程序。
运行的时候应是java A 123
String args[]输入要为整数
如果不是的话,也就是你说的出错,抛出NumberFormatException。
楼主如果想得到字符的ascii码的话,Interger.parseInt并不提供此功能。。
你可以用很简单的(int)强制转换得到ascii码。
你写的代码要求输入的参数必须是数字,不然就会出现类型转换错误;
如果你想要得到a-->97的结果,那你就需求稍微改一下程序,先将字符串拆成一个个字符,然后用getAsc()得到每个字符的asc码。
byte[] gc = st.getBytes();
asnum = (int) gc[0];
return asnum;
}