在 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 ??
最佳答案
查看 java.util.Scanner,它可能会满足您的需求:
http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html
关于java - 为什么 Java 只接受字符串格式的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7588069/