Java 如何把一个数字拆开并乘以它的数字?

Java 如何把一个数字拆开并乘以它的数字?,java,c,loops,while-loop,numbers,Java,C,Loops,While Loop,Numbers,嗨,我用的是代码块,我是个新手, 我想把一个数字拆开,然后乘以它的数字 如果数字大于9,我想再一次把数字分开,相乘, 示例:如果我有数字126,那么新的数字将是1*2*6=12 但是12大于9,那么新的数字再次是1*2=2,2不大于9,然后退出循环 谢谢请尝试此程序: public class B { public static void main(String[] args) { String number = "126"; int c = 1;

嗨,我用的是代码块,我是个新手, 我想把一个数字拆开,然后乘以它的数字 如果数字大于9,我想再一次把数字分开,相乘, 示例:如果我有数字126,那么新的数字将是1*2*6=12 但是12大于9,那么新的数字再次是1*2=2,2不大于9,然后退出循环
谢谢

请尝试此程序:

public class B {

    public static void main(String[] args) {

        String number = "126";
        int c = 1;
        for (int j =  number.length() ; j >=0; j--) {
        int v = j;
            for (int i = 0; i < v; i++) {
                String s = ""+number.charAt(i);
                c = c*  Integer.parseInt(s.trim());
            }

            System.out.println("result is : "+c);    
            c = 1;
        }        
}
}
公共B类{
公共静态void main(字符串[]args){
字符串编号=“126”;
int c=1;
对于(int j=number.length();j>=0;j--){
int v=j;
对于(int i=0;i
您需要使用一个函数计算您输入的数字的每个数字,然后迭代直到计算出零。类似这样的操作会起作用,然后只返回switch语句的其余部分:

quotient = number / 10; 
remainder = number % 10; 
switch(remainder)
{
    case 1:
        return 1
        break;
}

您可以创建一个方法来查找字符串中的数字字符,如下所示:

public static int Match(String pattern, String word) {
    int abc = 1;
    Matcher m = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(word);
    while (m.find()) {

        abc *= Integer.parseInt(m.group());
        System.out.println("val of abc when (m=" + m.group() + ") - " + abc);

    }
    System.out.println("*************************");
    return abc;
}
    String word = "126 Florida Beach";
    String pattern = "\\d";

    int abc = 1;
    do {
        System.out.println("word at  - " + word);
        System.out.println("abc at - " + abc);
        abc = Match(pattern, word);
        word = String.valueOf(abc);

    } while (abc > 9);
    System.out.println("Required Value - " + abc);
然后可以检查main方法中的给定字符串,直到得到所需的数字:

public static int Match(String pattern, String word) {
    int abc = 1;
    Matcher m = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(word);
    while (m.find()) {

        abc *= Integer.parseInt(m.group());
        System.out.println("val of abc when (m=" + m.group() + ") - " + abc);

    }
    System.out.println("*************************");
    return abc;
}
    String word = "126 Florida Beach";
    String pattern = "\\d";

    int abc = 1;
    do {
        System.out.println("word at  - " + word);
        System.out.println("abc at - " + abc);
        abc = Match(pattern, word);
        word = String.valueOf(abc);

    } while (abc > 9);
    System.out.println("Required Value - " + abc);

C语言中的一个简单解决方案:

int muldigits(unsigned int n) {
    while (n > 9) {
        unsigned int m = n;
        for (n = 1; m; n *= m % 10, m /= 10);
    }
    return n;
}

int-num;int newnum=1;printf(“输入数字”);scanf(“%d”和&num);虽然(newnum>9){digit=num%10;newnum*=digit;num/=10;}@madushanpera我知道我一直在将所有旧数字的结果添加到新数字中:\@我可以知道原因吗???