访问字符串中的单个字母/数字中的数字-Java

访问字符串中的单个字母/数字中的数字-Java,java,string,Java,String,我有数字输入(11位),我需要对每个数字执行一些操作(例如:1乘以5,2乘以3,等等)。我如何在Java中做到这一点?有没有一种简单的方法可以访问单个字母/数字?还有其他方法吗?您可以使用.charAt()从字符串中获取字符。然后使用Character.getNumericValue()可以将字符转换为整数 像这样: String string = "1434347633"; int digit1 = Character.getNumericValue(string.charAt(1));

我有数字输入(11位),我需要对每个数字执行一些操作(例如:1乘以5,2乘以3,等等)。我如何在Java中做到这一点?有没有一种简单的方法可以访问单个字母/数字?还有其他方法吗?

您可以使用
.charAt()
从字符串中获取字符。然后使用
Character.getNumericValue()
可以将字符转换为整数

像这样:

String string = "1434347633";

int digit1 = Character.getNumericValue(string.charAt(1));
您可以使用
.charAt()
从字符串中获取字符。然后使用
Character.getNumericValue()
可以将字符转换为整数

像这样:

String string = "1434347633";

int digit1 = Character.getNumericValue(string.charAt(1));
您可以使用
.charAt()
从字符串中获取字符。然后使用
Character.getNumericValue()
可以将字符转换为整数

像这样:

String string = "1434347633";

int digit1 = Character.getNumericValue(string.charAt(1));
您可以使用
.charAt()
从字符串中获取字符。然后使用
Character.getNumericValue()
可以将字符转换为整数

像这样:

String string = "1434347633";

int digit1 = Character.getNumericValue(string.charAt(1));

将该数字输入转换为
String
数据类型,以便将其解释为字符串

int numbers = 1122334455; // integer won't be able to save this much data, 
// just for example ok, 
String numberString = numbers.toString();
foreach (char number in numberString) {
  // do work on each of the single character in the string.
}

你应该根据具体情况来解决这些问题

将该数字输入转换为
字符串
数据类型,以便将其解释为字符串

int numbers = 1122334455; // integer won't be able to save this much data, 
// just for example ok, 
String numberString = numbers.toString();
foreach (char number in numberString) {
  // do work on each of the single character in the string.
}

你应该根据具体情况来解决这些问题

将该数字输入转换为
字符串
数据类型,以便将其解释为字符串

int numbers = 1122334455; // integer won't be able to save this much data, 
// just for example ok, 
String numberString = numbers.toString();
foreach (char number in numberString) {
  // do work on each of the single character in the string.
}

你应该根据具体情况来解决这些问题

将该数字输入转换为
字符串
数据类型,以便将其解释为字符串

int numbers = 1122334455; // integer won't be able to save this much data, 
// just for example ok, 
String numberString = numbers.toString();
foreach (char number in numberString) {
  // do work on each of the single character in the string.
}

你应该根据具体情况来解决这些问题

如果您不想将数字转换为字符串,那么有一个简单的技巧

digit = number % 10 
将为您提供最右边的数字

number = number / 10 
将从数字中删除最右边的数字

因此,您可以在循环中运行,直到数字达到0

while(0 < number)
{
    int digit = number % 10;
    number = number / 10;

    // do here an operation on the digits
}
while(0
您可以使用for循环来帮助您计数。比如说

for(int index = 0; 0 < number; ++index, number /= 10)
{
    int digit = number % 10;
    // do an operation on the number according to the 'index' variable
}
for(int-index=0;0

这里有一个类似的问题

如果你不想将数字转换成字符串,那么有一个简单的技巧

digit = number % 10 
将为您提供最右边的数字

number = number / 10 
将从数字中删除最右边的数字

因此,您可以在循环中运行,直到数字达到0

while(0 < number)
{
    int digit = number % 10;
    number = number / 10;

    // do here an operation on the digits
}
while(0
您可以使用for循环来帮助您计数。比如说

for(int index = 0; 0 < number; ++index, number /= 10)
{
    int digit = number % 10;
    // do an operation on the number according to the 'index' variable
}
for(int-index=0;0

这里有一个类似的问题

如果你不想将数字转换成字符串,那么有一个简单的技巧

digit = number % 10 
将为您提供最右边的数字

number = number / 10 
将从数字中删除最右边的数字

因此,您可以在循环中运行,直到数字达到0

while(0 < number)
{
    int digit = number % 10;
    number = number / 10;

    // do here an operation on the digits
}
while(0
您可以使用for循环来帮助您计数。比如说

for(int index = 0; 0 < number; ++index, number /= 10)
{
    int digit = number % 10;
    // do an operation on the number according to the 'index' variable
}
for(int-index=0;0

这里有一个类似的问题

如果你不想将数字转换成字符串,那么有一个简单的技巧

digit = number % 10 
将为您提供最右边的数字

number = number / 10 
将从数字中删除最右边的数字

因此,您可以在循环中运行,直到数字达到0

while(0 < number)
{
    int digit = number % 10;
    number = number / 10;

    // do here an operation on the digits
}
while(0
您可以使用for循环来帮助您计数。比如说

for(int index = 0; 0 < number; ++index, number /= 10)
{
    int digit = number % 10;
    // do an operation on the number according to the 'index' variable
}
for(int-index=0;0

下面是一个类似问题的类似示例

如果您希望通过索引访问数字而不转换为字符串,则可以使用这两个函数
length
digitAt

public class DigitAt {

    public static int length(long v) {
        return (int) Math.ceil(Math.log10(v));
    }

    public static int digitAt(long v, int digit) {
        return (int) ((v / (long) Math.pow(10, length(v) - digit - 1)) % 10);
    }

    public static void main(String[] args) {
        System.out.println("Digits: " + length(1234567));
        System.out.println(digitAt(1234567, 0));
        System.out.println(digitAt(1234567, 1));
        System.out.println(digitAt(1234567, 6));
    }
}

如果要按索引访问数字而不转换为字符串,可以使用以下两个函数
length
digitAt

public class DigitAt {

    public static int length(long v) {
        return (int) Math.ceil(Math.log10(v));
    }

    public static int digitAt(long v, int digit) {
        return (int) ((v / (long) Math.pow(10, length(v) - digit - 1)) % 10);
    }

    public static void main(String[] args) {
        System.out.println("Digits: " + length(1234567));
        System.out.println(digitAt(1234567, 0));
        System.out.println(digitAt(1234567, 1));
        System.out.println(digitAt(1234567, 6));
    }
}

如果要按索引访问数字而不转换为字符串,可以使用以下两个函数
length
digitAt

public class DigitAt {

    public static int length(long v) {
        return (int) Math.ceil(Math.log10(v));
    }

    public static int digitAt(long v, int digit) {
        return (int) ((v / (long) Math.pow(10, length(v) - digit - 1)) % 10);
    }

    public static void main(String[] args) {
        System.out.println("Digits: " + length(1234567));
        System.out.println(digitAt(1234567, 0));
        System.out.println(digitAt(1234567, 1));
        System.out.println(digitAt(1234567, 6));
    }
}

如果要按索引访问数字而不转换为字符串,可以使用以下两个函数
length
digitAt

public class DigitAt {

    public static int length(long v) {
        return (int) Math.ceil(Math.log10(v));
    }

    public static int digitAt(long v, int digit) {
        return (int) ((v / (long) Math.pow(10, length(v) - digit - 1)) % 10);
    }

    public static void main(String[] args) {
        System.out.println("Digits: " + length(1234567));
        System.out.println(digitAt(1234567, 0));
        System.out.println(digitAt(1234567, 1));
        System.out.println(digitAt(1234567, 6));
    }
}

有很多方法可以做到这一点,比如:

        int a = 12345;
        int b;
        while(a>0)
        {
            b = a%10;
            System.out.print(b + " ");
            a = a/10;
        }
在这里,它以相反的顺序给你数字,就像你会得到b=5,然后b=4

你可以操纵它们

其他方式

    int d = 12345;
    String str = String.valueOf(d);
    for(int i=0;i<str.length();i++)
    {
        char c = str.charAt(i);
        System.out.print(Character.getNumericValue(c) * 10 + " ");

    }

有很多方法可以做到这一点,比如:

        int a = 12345;
        int b;
        while(a>0)
        {
            b = a%10;
            System.out.print(b + " ");
            a = a/10;
        }
在这里,它以相反的顺序给你数字,就像你会得到b=5,然后b=4

你可以操纵它们

其他方式

    int d = 12345;
    String str = String.valueOf(d);
    for(int i=0;i<str.length();i++)
    {
        char c = str.charAt(i);
        System.out.print(Character.getNumericValue(c) * 10 + " ");

    }

有很多方法可以做到这一点,比如:

        int a = 12345;
        int b;
        while(a>0)
        {
            b = a%10;
            System.out.print(b + " ");
            a = a/10;
        }
在这里,它以相反的顺序给你数字,就像你会得到b=5,然后b=4

你可以操纵它们

其他方式

    int d = 12345;
    String str = String.valueOf(d);
    for(int i=0;i<str.length();i++)
    {
        char c = str.charAt(i);
        System.out.print(Character.getNumericValue(c) * 10 + " ");

    }

有很多方法可以做到这一点,比如:

        int a = 12345;
        int b;
        while(a>0)
        {
            b = a%10;
            System.out.print(b + " ");
            a = a/10;
        }
在这里,它以相反的顺序给你数字,就像你会得到b=5,然后b=4

你可以操纵它们

其他方式

    int d = 12345;
    String str = String.valueOf(d);
    for(int i=0;i<str.length();i++)
    {
        char c = str.charAt(i);
        System.out.print(Character.getNumericValue(c) * 10 + " ");

    }
公共字符串stringAfterOperations(字符串数字){
ArrayList z=新