简体   繁体   中英

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". 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").

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.

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.

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 .

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() . You can use % 10 to ensure that the count wraps after the ninth digit. Also, I'd use a 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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