简体   繁体   English

While循环不会退出

[英]While loop does not exit

While trying to write a simple java code to replace a particular character in a given string by using loop I have been much confused to find the mistake. 在尝试编写简单的Java代码以使用循环替换给定字符串中的特定字符时,我很困惑地发现了错误。 Details are as below: 详细信息如下:

INPUT String : "123qq11 1q1 11q1 1qq11 1q1 11q1"


REQUIRED OUTPUT: "123QQ11 1Q1 11Q1 1QQ11 1Q1 11Q1"


GOT OUTPUT:   "Q23QQ11 1Q1 11Q1 1QQ11 1Q1 11Q1" as infinite loop

ALGORITHM: Replacing 'q' by 'Q' in given String. 算法:在给定的字符串中用“ Q”替换“ q”。

My code to be corrected is: 我要更正的代码是:

public class Rani {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("123qq11 1q1 11q1 1qq11 1q1 11q1");
        int j = 0;
        int i = 0;
        while (i < sb.length()) {
            while (i + 1 < sb.length()) { // while 2nd
                i++;

                if (sb.charAt(i) == 'q') {
                    j = i;
                    break;
                } else {
                    break;
                }
            }

            sb.replace(j, j + 1, "Q");

            System.out.println(sb);
        }
    }
}

Being new comer to java and programming I have been failed to manage the corrections. 作为Java和编程的新手,我无法管理这些更正。

Firstly you don't need 2 while loops. 首先,您不需要2 while循环。

Secondly i stays at sb.length() - 1 forever making it an infinite loop. 其次, i永远停留在sb.length() - 1上,从而使其成为无限循环。

You could use a replace or replaceAll instead. 您可以使用replacereplaceAll代替。

String st = "123qq11 1q1 11q1 1qq11 1q1 11q1"
st = st.replace("q", "Q");

If you want to do it with while loops. 如果您想用while循环来做。

int i = 0; 
while(i < sb.length())
{
 if(sb.charAt(i) == 'q')
  sb.setCharAt(i, 'Q');
i++;
}

Here is the working code 这是工作代码

 public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("123qq11 1q1 11q1 1qq11 1q1 11q1");
    int j = 0;
    int i = 0;
    while (i < sb.length()) {
        while (i < sb.length()) { // while 2nd

            if (sb.charAt(i) == 'q') {
                j = i;
                break;
            }
            i++;
        }

        sb.replace(j, j + 1, "Q");
        System.out.println(sb);
    }
}

Output: 123QQ11 1Q1 11Q1 1QQ11 1Q1 11Q1 输出:123QQ11 1Q1 11Q1 1QQ11 1Q1 11Q1

If you don't want to use replace you can just do this. 如果您不想使用replace,则可以执行此操作。

for (int i = 0; i < st.length(); i++)
    if (st.charAt(i) == 'q')
        st.setCharAt(i, 'Q');
public static void main (String[] args) {
         StringBuilder sb = new StringBuilder("123qq11 1q1 11q1 1qq11 1q1 11q1");
         String strnew = "";
         for(int i=0;i<sb.length();i++)
         {
             if(sb.charAt(i)=='q')
             {      
                 strnew=strnew+"Q";

             }
             else
                 strnew=strnew+sb.charAt(i);
         }

         System.out.println(strnew);
    }

in your code you are telling i to increment too soon. 在您的代码中,您告诉我过快增加。 in the inner loop i only gets to 30 while the length is actually 31. if you move the i++ to the end of the first loop but our side of the inner loop it should work they way you want. 在内部循环中, i只能达到30,而长度实际上是31。如果将i++移到第一个循环的末尾,但在我们内部循环的那一侧,它应该按您想要的方式工作。 like so: 像这样:

...
if (sb.charAt(i) == 'q') {
                j = i;
                break;
            }
            i++;
        }
...

the only problem with that is you are always replacing the first character of the string. 唯一的问题是您总是替换字符串的第一个字符。 the first number "1" is being replaced with a "Q". 第一个数字“ 1”被替换为“ Q”。 becasue you are using j to replace "q" it is always the character at the 0th slot. 因为您使用j代替“ q”,所以它始终是第0个字符。

You could even make it simpler buy just using one loop: 您甚至可以只使用一个循环就可以使购买更简单:

while (i<sb.length()){
    if (sb.charAt(i) == 'q'){
        sb.replace(i, i + 1, "Q");
    }
    i++;
}
System.out.println(sb);

or you could use a for loop: 或者您可以使用for循环:

for (i =0; i<sb.length();i++){
    if (sb.charAt(i)=='q'){
        sb.replace(i, i+1, "Q");
    }       
}
System.out.println(sb);

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

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