将大数分成单独的数字并存储它们
Dividing large number into separate digits and store them
因此,例如,如果我有一个大数字,比如 18750409。我想将这个数字分成单独的数字 1,8,7,5,0,4,0,9。但是我不想马上把它们打印出来,我想把它们存储在某个地方,比如数字 A 代表 1,数字 B 代表 8。(可能在一个数组中?)所以我以后可以用这些数字做更多的事情在程序中打印出结果之前。
感谢任何帮助!
package test;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author shalom
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int num,
temp, //it holds the remainder value
n; //it will be used for seperating the integer
int[] array;
String s = ""; // if you want to store the number in string
System.out.println("Enter the number"); //Taking value from user for any number
num = new Scanner(System.in).nextInt();
n = num;
while (n>0) {
temp = n%10; //remainder extracted
s = temp + s; //concatenated with s
n = n / 10; //to get the next digit to be separated
}
array = new int[s.length()]; //finally if you want to store the values in array initiliaze the array with lenght of s
for (int i = 0; i < s.length(); i++)
array[i] = Integer.parseInt("" + s.charAt(i)); // values are stored in array
}
}
您可以使用ArrayList
来存储结果。并使用 digitArray.get(i-1)
检索单个数字 i
int n = 18750409;
List<Integer> digitArray = new ArrayList<>();
while(n > 0 ){
digitArray.add(n%10);
n = n/10;
}
//to get individual digits
digit3 = digitArray.get(2);
int hjk=326543;
String cvbn = new String(String.valueOf(hjk));
System.out.println(cvbn);
char xcv[] = cvbn.toCharArray();
int cv[] = new int[xcv.length];
for(int hj = 0;hj<xcv.length;hj++)
{
cv[hj] = Integer.parseInt(String.valueOf(xcv[hj]));
}
希望我的代码对您有所帮助。快乐编码
1) 获取号码的长度(位数)(可以通过将号码转换为字符串并通过调用 .length();
获取长度来完成)
int number = ...;
String my_string = "" + number;
int numberLength = my_string.length();
2) 使用 for 循环遍历您的字符串,并创建一个单独的字符数组,当您使用 charAt() 遍历字符串的每个字符时,将它们存储在该数组中:
char[] charArray;
charArray = new char[numberLength];
for(int i = 0; i < (numberLength - 1); i++) // -> Remember, -1 because arrays start at 0!
{
charArray[i] = my_string.charAt(i);
}
现在你有一个字符形式的数字数组。要将它们转换回整数,只需循环遍历该数组并将字符转换回数字。
注意:您也可以将数字存储为字符串。
如果您使用的是字符,则转换为 int:使用 getNumericValue() -> 这在技术上 returns unicode 数值,但数字的 unicode 就是那个数字。
int[] digitArray;
digitArray = new int[numberLength];
for (int i =0; i< numberLength; i++)
{
digitArray[i] = charArray[i].getNumericValue();
}
就是这样。 :)
(抱歉,如果某些 java 语法已关闭;我已经有一段时间没有完成 java。
因此,例如,如果我有一个大数字,比如 18750409。我想将这个数字分成单独的数字 1,8,7,5,0,4,0,9。但是我不想马上把它们打印出来,我想把它们存储在某个地方,比如数字 A 代表 1,数字 B 代表 8。(可能在一个数组中?)所以我以后可以用这些数字做更多的事情在程序中打印出结果之前。
感谢任何帮助!
package test;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author shalom
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int num,
temp, //it holds the remainder value
n; //it will be used for seperating the integer
int[] array;
String s = ""; // if you want to store the number in string
System.out.println("Enter the number"); //Taking value from user for any number
num = new Scanner(System.in).nextInt();
n = num;
while (n>0) {
temp = n%10; //remainder extracted
s = temp + s; //concatenated with s
n = n / 10; //to get the next digit to be separated
}
array = new int[s.length()]; //finally if you want to store the values in array initiliaze the array with lenght of s
for (int i = 0; i < s.length(); i++)
array[i] = Integer.parseInt("" + s.charAt(i)); // values are stored in array
}
}
您可以使用ArrayList
来存储结果。并使用 digitArray.get(i-1)
i
int n = 18750409;
List<Integer> digitArray = new ArrayList<>();
while(n > 0 ){
digitArray.add(n%10);
n = n/10;
}
//to get individual digits
digit3 = digitArray.get(2);
int hjk=326543;
String cvbn = new String(String.valueOf(hjk));
System.out.println(cvbn);
char xcv[] = cvbn.toCharArray();
int cv[] = new int[xcv.length];
for(int hj = 0;hj<xcv.length;hj++)
{
cv[hj] = Integer.parseInt(String.valueOf(xcv[hj]));
}
希望我的代码对您有所帮助。快乐编码
1) 获取号码的长度(位数)(可以通过将号码转换为字符串并通过调用 .length();
获取长度来完成)int number = ...;
String my_string = "" + number;
int numberLength = my_string.length();
2) 使用 for 循环遍历您的字符串,并创建一个单独的字符数组,当您使用 charAt() 遍历字符串的每个字符时,将它们存储在该数组中:
char[] charArray;
charArray = new char[numberLength];
for(int i = 0; i < (numberLength - 1); i++) // -> Remember, -1 because arrays start at 0!
{
charArray[i] = my_string.charAt(i);
}
现在你有一个字符形式的数字数组。要将它们转换回整数,只需循环遍历该数组并将字符转换回数字。 注意:您也可以将数字存储为字符串。
如果您使用的是字符,则转换为 int:使用 getNumericValue() -> 这在技术上 returns unicode 数值,但数字的 unicode 就是那个数字。
int[] digitArray;
digitArray = new int[numberLength];
for (int i =0; i< numberLength; i++)
{
digitArray[i] = charArray[i].getNumericValue();
}
就是这样。 :) (抱歉,如果某些 java 语法已关闭;我已经有一段时间没有完成 java。