简体   繁体   English

在循环中将变量重置为零

[英]Resetting a variable to zero while in a loop

I have this code that allows me to take a string and replace all the vowels with an increasing number. 我有这个代码,允许我取一个字符串,并用越来越多的数字替换所有元音。

For example, "abababababababababababa" would give me "0b1b2b3b4b5b6b7b8b9b10b11". 例如,“abababababababababababa”会给我“0b1b2b3b4b5b6b7b8b9b10b11”。 I am trying to keep it so that the number, once it goes past 9, resets to zero and starts counting back up again (so that "0b1b2b3b4b5b6b7b8b9b10b11" would instead read "0b1b2b3b4b5b6b7b8b9b0b1"). 我试图保持它,以便数字一旦超过9,重置为零并开始重新计数(因此“0b1b2b3b4b5b6b7b8b9b10b11”将改为读取“0b1b2b3b4b5b6b7b8b9b0b1”)。

I can't figure out a way to do it within the loop that isn't fairly convoluted. 我无法想出一种在不太复杂的循环中做到这一点的方法。 If there is some way of achieving this that someone wouldn't mind divulging, that would be greatly appreciated. 如果有某种方法可以实现这一点,有人不介意泄露,那将非常感激。

public static String getNumberString( String s)
{
 String word = s;
 String word1 = word.replaceAll("[AEIOUaeiou]", "@");
 int c = 0;

 String word2 =  word1;
 for( c = 0; c <= word.length(); c++)
 {
       word2 = word2.replaceFirst("@", Integer.toString(c));
 }

 return word2;
}

You can use Modulus of 10 (% 10) , this will wrap the count after the ninth digit to starting again. 您可以使用Modulus of 10 (% 10) ,这将在第9位之后包装计数再次开始。

public static String getNumberString(String s) {
        String word = s;
        String word1 = word.replaceAll("[AEIOUaeiou]", "@");
        int c = 0;

        String word2 = word1;
        for (c = 0 ; c <= word.length() ; c++) {
            word2 = word2.replaceFirst("@", Integer.toString(c % 10));
        }

        return word2;
    }

output 产量

0b1b2b3b4b5b6b7b8b9b0b1

使用modulo怎么样?

word2 = word2.replaceFirst("@", Integer.toString(c % 10));

Try using modulus with ten. 尝试使用模数十。 It will cycle through because as it gets to a multiple of ten, the modulus will be 0 again. 它将循环通过,因为当它达到十的倍数时,模数将再次为0。

You can use a different variable other than c as the loop and by using and if condition you can check when c become 9 and reset it to 0 . 您可以使用除c之外的其他变量作为循环,通过使用和if条件,您可以检查c何时变为9并将其重置为0

public static String getNumberString( String s)
{
 String word = s;
 String word1 = word.replaceAll("[AEIOUaeiou]", "@");
 int c = 0;

 String word2 =  word1;
 for(int i = 0; i <= word.length(); i++)
 {
       word2 = word2.replaceFirst("@", Integer.toString(c));
       if(c == 9) {
           c = 0;
       } else {
           c++;
       }
 }

 return word2;
}

在你的循环中,你可以做到

for (c = 0; c <= word.length(); c = (c + 1) % 10) {...}

您必须使用c%10而不是c,如下代码:

word2 = word2.replaceFirst("@", Integer.toString(c % 10));

而不是用c替换你可以使用c % 10

I would use String.toCharArray() . 我会使用String.toCharArray() You can use % 10 to ensure that the count wraps after the ninth digit. 您可以使用% 10来确保count在第九位之后换行。 Also, I'd use a StringBuilder . 另外,我使用StringBuilder Something like, 就像是,

public static String getNumberString(String str) {
    int count = 0;
    StringBuilder sb = new StringBuilder();
    if (str != null) {
        for (char ch : str.toCharArray()) {
            switch (Character.toLowerCase(ch)) {
            case 'a': case 'e': case 'i': case 'o': case 'u':
                sb.append(count);
                ++count;
                count %= 10;
                // or maybe, count = (count > 9) ? 0 : count;
                break;
            default:
                sb.append(ch);
            }
        }
    }
    return sb.toString();
}

Then you can test it like 然后你可以测试它

public static void main(String[] args) {
    String out = getNumberString("abababababababababababa");
    String expected = "0b1b2b3b4b5b6b7b8b9b0b1";
    System.out.println(out.equals(expected));
}

Output is 输出是

true

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

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