简体   繁体   English

试图更好地掌握循环逻辑。

[英]Trying to gain a better grasp of loop logic.

I hope your Sunday is going well. 我希望你的星期天过得愉快。 So my goal in this little program is to print a new string with indexes [0,1] [4,5]...[12,13]... The loop functions only with an even numbered word that's greater than 4 letters. 所以我在这个小程序中的目标是打印一个索引为[0,1] [4,5] ... [12,13] ...的新字符串。该循环仅对大于4个字母的偶数个单词起作用。 Why is this? 为什么是这样? Any advice on how to polish this turd would be greatly appreciated. 任何有关如何抛光此草皮的建议将不胜感激。 Thank you. 谢谢。

import java.util.Scanner;

public class LoopPractice {
public static void main(String[] args) {

  Scanner myScanner = new Scanner(System.in);

  System.out.print("Enter a String please: ");
  String str = myScanner.next();

  int count = 0;
  int x = 0;
  int y = 1;
  String emptyStr = "";

     while ( count != str.length() ) {

        emptyStr += str.charAt(x) + "" +  str.charAt(y);
        x += 4;
        y += 4;
        count += emptyStr.length();
     }
  System.out.print(emptyStr);

}
}

I am not 100% sure of your question, but the problem seems to be lying here: x += 4; y += 4; 我不是100%肯定您的问题,但是问题似乎就在这里: x += 4; y += 4; x += 4; y += 4; . You are incrementing those two variables by 4 each time (0, 4, 8 for X and 1, 5, 9 for Y). 您每次将这两个变量递增4(X分别为0、4、8,Y则为1、5、9)。

When you are using the .charAt function, you are most likely getting an error which denotes some out of bounds issue since the values for X and Y are larger than the length of the string. 当您使用.charAt函数时,您很可能会收到一条错误消息,该错误消息表示某些超出范围的问题,因为X和Y的值大于字符串的长度。

You will need to change the way that X and Y are incremented for you to stop getting that error. 您将需要更改X和Y递增的方式,以停止出现该错误。

While condition, should check if you are not overflow string index: 条件时,应检查您是否没有溢出字符串索引:

import java.util.Scanner;

public class LoopPractice {
public static void main(String[] args) {

  Scanner myScanner = new Scanner(System.in);

  System.out.print("Enter a String please: ");
  String str = myScanner.next();

  int x = 0;
  int y = 1;
  String emptyStr = "";

     while ( y <= str.length() ) {

        emptyStr += str.charAt(x) + "" +  str.charAt(y);
        x += 4;
        y += 4;
     }
  System.out.print(emptyStr);

}
}

This is all what you need : 这就是您所需要的:

System.out.print("Enter a String please: ");
String str = myScanner.next();
while(!str.isEmpty()){
          System.out.println(str.charAt(0)+""+str.charAt(1));
          if(str.length()>4)
              str = str.substring(4);
          else
              str = "";
     }

The problem is this line 问题是这条线

count += emptyStr.length();

Your emptyStr gets longer by two characters every time. 您的emptyStr都会延长两个字符。 So the first time through, you add 2 to count . 因此,第一次通过,您要添加2来count The next time, you add 4, then 6, and so on. 下次,您添加4,然后添加6,依此类推。 So count takes the values 0, 2, 6, 12, 20 and so on. 因此count取值为0、2、6、12、20等。

If str.length() isn't one of those values, your loop will never end. 如果str.length()不是这些值之一,则循环将永远不会结束。

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

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