在java中,当我们从控制台获取输入时,我们得到一个字符串,即使我们想要一个整数作为输入,我们也会得到一个字符串格式的输入,然后我们使用几种方法将其转换为整数格式,比如Integer.parseInt()。因为C/C++也从控制台获取输入,但是我们直接从控制台获取整数值,我们不需要方法来转换它们。那么为什么java要遵循这么长的过程。** Java这种架构背后的原因是什么?
//In java we follow the following process
public static void main(String args[])
{int i = Integer.parseInt( args[0]);// here we get input in String format and then convert it
}
//In C++ we follow the following :
void main()
{int i;
cin>>i;
}
Both C/C++ and Java takes input form Console then why java takes it in String Format and C++ does not ??发布于 2011-09-29 02:59:01
Java -如果使用Scanner类,则可以获得所需数据类型的输入。这不仅仅是String java所接受的。
发布于 2011-09-29 02:55:46
查看java.util.Scanner,它可能会满足您的需求:
http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html
发布于 2011-09-29 02:56:45
如果你用C语言从控制台获取一个整数,它将要求你对输入执行atoi()。
https://stackoverflow.com/questions/7588069
复制相似问题