简体   繁体   English

Android Java While循环不执行

[英]Android Java While loop not executing

In my javacode I am using while loop where a value is adding together and making it to a single digit .. for example if it is 2010 then 2+0+1+0 = 3 and if it is 2345 then 2+3+4+5 = 14 then 1 + 4 = 5 like that ... but at the same time this process is not valid for two numbers .. 11 & 22 if the coming value is 11 and 22 then no further addition like 1+1 =2 or 2+2 =4; 在我的javacode中,我使用while循环,其中一个值加在一起并使其成为一个数字..例如,如果它是2010,那么2 + 0 + 1 + 0 = 3,如果它是2345那么2 + 3 + 4 +5 = 14然后1 + 4 = 5就像那样...但同时这个过程对两个数字无效... 11和22如果即将到来的值是11和22那么没有进一步的加法,如1 + 1 = 2或2 + 2 = 4; they have to display it as both 11 and 22 ... I wrote code like below but it is not working.... anyone pls check the code and help me...what is the mistake... 他们必须将它显示为11和22 ......我编写的代码如下,但它不起作用....任何人请检查代码并帮助我...错误是什么...

private long getSum10(String text)
{
    long sum10 = 0;
    char[] name10 = new char[text.length()];
    name10 = text.toCharArray();
    for (int i = 0; i < text.length(); i++)
    {
        sum10 += value10(name10[i]);
    }
    while ((sum10 != 11) && (sum10 != 22) && (sum10 > 9))
    {
        sum10 = findDigitSum10(sum10);
    }
    return sum10;
}
private long findDigitSum10(long n)
{
    int sum10 = 0;
    while (n != 0)
    {
        sum10 += n % 10;
        n = n / 10;
    }
    return sum10;
}
private int value10(char a)
{
    switch (a)
    {
    case 'A':
        return 1;
    case 'B':
        return 2;
    case 'C':
        return 3;
    case 'D':
        return 4;
    case 'E':
        return 5;
    case 'F':
        return 6;
    case 'G':
        return 7;
    case 'H':
        return 8;
    case 'I':
        return 9;
    case 'J':
        return 1;
    case 'K':
        return 2;
    case 'L':
        return 3;
    case 'M':
        return 4;
    case 'N':
        return 5;
    case 'O':
        return 6;
    case 'P':
        return 7;
    case 'Q':
        return 8;
    case 'R':
        return 9;
    case 'S':
        return 1;
    case 'T':
        return 2;
    case 'U':
        return 3;
    case 'V':
        return 4;
    case 'W':
        return 5;
    case 'X':
        return 6;
    case 'Y':
        return 7;
    case 'Z':
        return 8;
    default:
        return 0;
    }
}

I would have implemented this in the following way 我会以下面的方式实现这一点

public static void main(String[] args) {
    System.out.println(getSum(12));
    System.out.println(getSum(11));
}
public static long getSum(int no){      
    if(no/10 == 0 || no%11 == 0){
        return no;
    }else{
        return getSum(no/10) + no%10;
    }
}
 while ((sum10!=11) && (sum10!=22) && (sum10>9))
{
}

make it 做了

if ((sum10 != 11) && (sum10 != 22) )
{
   sum10 = findDigitSum10(sum10);
}

change it to if condition .. I don see the reason why u have written it in while condition..the answer ur are goin to get from findDigitSum10(sum10) will alway be less than 10.. 把它改成条件 ..我不知道你为什么写条件的原因条件 ...你的答案是从findDigitSum10(sum10)得到的答案总是小于10 ..

Put a condition to check the value of number. 设置条件来检查数字的值。 If n is greater than 99 add digits else show the number as it is. 如果n大于99,则添加数字,否则显示数字。

Update your method like this: 像这样更新您的方法:

private long findDigitSum10(long n) {

    // TODO Auto-generated method stub
    int sum10 = 0;
    long temp = n;

    if (temp > 99) {

        while (n != 0) {
            sum10 += n % 10;
            n = n / 10;
        }
    }

    else {
        return n;
    }

    return sum10;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM