提问人:Asheesh Sahu 提问时间:7/17/2018 更新时间:7/17/2018 访问量:1731
java 中的 nextInt() 方法 [duplicate]
nextInt() method in java [duplicate]
问:
在 java 中,当我在 Netbeans Ide 上工作时,我看到了 nextInt(int radix) 的方法。我尝试实现这一点并传递了一个整数,但这给出了 InputMismatchException 的异常。我的代码如下:
import java.util.*;
import java.lang.*;
public class HelloWorld{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(2);
System.out.println(n);
}
我想知道什么是基数参数?另外,它有什么用?
答:
6赞
ernest_k
7/17/2018
#1
基数是解释数字的基数。您可以使用它使方法将数字解释为非十进制。nextInt
例如,以下内容需要二进制中的数字:
s.nextInt(2)
> 11111 //input
31 //result in decimal
这需要一个十六进制数:
s.nextInt(16)
> abc12 //input
703506 //result in decimal
您还会有兴趣看到使用基数的其他方法,例如 Integer.toString(int, int)、Integer.parseInt(String, int),它们格式化/解析指定基数中的数字(类似的方法由 、 等提供)Long
Short
下一个:如何将验证放在子数组中?
评论