Java数组嵌套的for循环,不清除第二个for循环以返回到第一个for循环。

huangapple go评论75阅读模式
英文:

Java Array nested for loops, not clearing second for to go back to the first

问题

以下是您提供的代码的翻译部分:

以下是嵌套的for循环

if(answer.equals("No")){
    
    for (int i = 0; i < array.length;i++) {
        
        if (array[i] != null) {
            for (int count = 0; array[i].charAt(i) != ' ';count++) {
                if(count == 0)
                System.out.println(array[i]+" ");
            }
        }
    }
       
}

主要代码供参考:

import java.util.*;
import java.util.Scanner;

public class StringsWithSpaces {

public static void main(String[] args) 
{

    Scanner s = new Scanner(System.in);
    String[] array = new String[20];
    System.out.println("Please enter anything..., or type QUIT to quit.");
    
    for (int i = 0; i < array.length; i++) {
        array[i] = s.nextLine();
        boolean result = Arrays.stream(array).anyMatch("QUIT"::equals);
       if(result) 
       {
           break;
       }
    }
    String str = null;
    int len = -1;
    
    
    System.out.println("Would you like to display strings with No Spaces, One Space or More? Type No, One, More to see the results: ");
    String answer = s.nextLine();
    if(answer.equals("No")){
        
        for (int i = 0; i < array.length;i++) {
            
            if (array[i] != null) {
                for (int count = 0; array[i].charAt(i) != ' ';count++) {
                    if(count == 0)
                    System.out.println(array[i]+" ");
                }
            }
        }
           
    }
        else if(answer.equals("One"))
        {
            for (int i = 0; i < array.length;i++) {
                int count = 0;
                if (array[i] != null) {
                    if (array[i].charAt(i) != ' ') {
                        count++;
                        System.out.println(count);
                    }
                    
                        //System.out.print(array[i] + " ");   
                }
             }
        }
        else
            System.out.println("No values to show");

    System.out.println();
}   

}

希望这有助于您理解代码。如果您有任何进一步的问题,请随时提出。

英文:

The for loops in the code below are first looping through the user entries, then checking the number of spaces " " in the string. After checking through them a question is asked if they want to display strings with no spaces, one, or more. I don't think the loop is making it past the first instance of the for loop as the second should be looking for a 0 value on the count variable. Below is the nested for loops:

     if(answer.equals(&quot;No&quot;)){
for (int i = 0; i &lt; array.length;i++) {
if (array[i] != null) {
for (int count = 0; array[i].charAt(i) != &#39; &#39;;count++) {
if(count == 0)
System.out.println(array[i]+&quot; &quot;);
}
}
}
}

Primary code for reference:

    import java.util.*;
import java.util.Scanner;
public class StringsWithSpaces {
public static void main(String[] args) 
{
Scanner s = new Scanner(System.in);
String[] array = new String[20];
System.out.println(&quot;Please enter anything..., or type QUIT to quit.&quot;);
for (int i = 0; i &lt; array.length; i++) {
array[i] = s.nextLine();
boolean result = Arrays.stream(array).anyMatch(&quot;QUIT&quot;::equals);
if(result) 
{
break;
}
}
String str = null;
int len = -1;
System.out.println(&quot;Would you like to display strings with No Spaces, One Space or More? Type No, One, More to see the results: &quot;);
String answer = s.nextLine();
if(answer.equals(&quot;No&quot;)){
for (int i = 0; i &lt; array.length;i++) {
if (array[i] != null) {
for (int count = 0; array[i].charAt(i) != &#39; &#39;;count++) {
if(count == 0)
System.out.println(array[i]+&quot; &quot;);
}
}
}
}
else if(answer.equals(&quot;One&quot;))
{
for (int i = 0; i &lt; array.length;i++) {
int count = 0;
if (array[i] != null) {
if (array[i].charAt(i) != &#39; &#39;) {
count++;
System.out.println(count);
}
//System.out.print(array[i] + &quot; &quot;);   
}
}
}
else
System.out.println(&quot;No values to show&quot;);
System.out.println();
}	

}

I have looked for something similar, but could only find other languages. Thanks for all the help.

答案1

得分: 2

在第一组嵌套循环中,您基本上每次都在检查每个单词的相同字符。您应该使用count作为charAt方法的参数,而不是i中的任何索引;
另外,只有当count等于单词长度减一时,才应该打印出该单词。

这是代码:

if(answer.equals("No")){

    for (int i = 0; i < array.length; i++) {

        if (array[i] != null) {
            for (int count = 0; array[i].charAt(count) != ' '; count++) {
                if(count == array[i].length() - 1) {
                    System.out.println(array[i]);
                    break;
                }
            }
        }
    }

}

我还重新编写了第二组嵌套循环,代码本身应该可以解释:

else if(answer.equals("One")) {
    for (int i = 0; i < array.length; i++) {
        int count = 0;
        for (int j = 0; j < array[i].length(); j++) {
            if (Character.isSpaceChar(array[i].charAt(j)))
                count++;
        }
        if (count == 1) {
            System.out.println(array[i]);
        }
    }
}

编辑:我忘记提到我添加了您忘记的break语句。它确保在达到字符串的最后一个字符时退出for循环。如果没有此语句,每次到达字符串末尾时,应用程序都会因为IndexOutOfBoundsException而崩溃。

英文:

In the first set of nested loops you're basically checking the same character of each word each time. You should use count as the parameter for the charAt method instead of whatever index is in i;
Also you should only print out the word when count equals the length of the word minus one.

Here it is:

if(answer.equals(&quot;No&quot;)){
for (int i = 0; i &lt; array.length;i++) {
if (array[i] != null) {
for (int count = 0; array[i].charAt(i) != &#39; &#39; ;count++) {
if(count == array[i].length()-1) {
System.out.println(array[i]);
break;
}
}
}
}
}

I also rewrote the second set of nested loops, it should explain itself:

else if(answer.equals(&quot;One&quot;))
{
for (int i = 0; i &lt; array.length;i++) {
int count = 0;
for (int j = 0; j &lt; array[i].length(); j++) {
if (Character.isSpaceChar(array[i].charAt(j)))
count++;
}
if (count == 1) {
System.out.println(array[i]);
}
}
}

EDIT: I forgot to mention that I added the break statement that you forgot. It ensures that the for loop is exited when you reach the last character in the string. Without this statement your application would crash each time it hits the end of a string because of an IndexOutOfBoundsException.

huangapple
  • 本文由 发表于 2020年10月20日 05:20:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64435234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定