我将如何接受Integer
用户输入,502
并将其以数组(如)的形式存储arr[0]=5
,arr[1]=0,arr[2]=2
并分别进行访问。
您可以通过使用做到这一点Integer.toString()
功能,以您的转换Integer
成一个String
,然后使用String.toCharArray()
功能,将改变你的String
成char[]
。
public class Program {
public static void main(String[] args) {
// Declare your scanner
Scanner sc = new Scanner(System.in);
// Waits the user to input a value in the console
Integer integer = sc.nextInt();
// Close your scanner
sc.close();
// Put your string into a char array
char[] array = integer.toString().toCharArray();
// Print the result
System.out.println(Arrays.toString(array));
}
}
输入 : 502
输出 : [5, 0, 2]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句