!= 在 while 循环中未正常工作。

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

!= is not funtioning properly in while loop

问题

import java.util.Scanner;

class piglatin{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("输入一个字符串");
        String s=in.nextLine(),n1="",n2="",n3="";
        int i;
        for(i=0;i<s.length();i++){
            if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='o'||s.charAt(i)=='i'||s.charAt(i)=='u')
                while (s.charAt(i)!=' '&& i!=s.length()){     // 这里是问题的所在
                    n1+=s.charAt(i);
                    i++;
                }
            else if(s.charAt(i)==' '){
                n2="";
                i++;
            }
            else
                n2+=s.charAt(i);

            n3=n3+n1+n2+"ay ";
        }
        System.out.println(n3);
    }
}

在这里,当i的值与长度相匹配时,循环应该终止,但它总是显示越界异常。我的错误是什么?我已经使用了&&,逻辑上应该是假 && 真 = 假。请帮忙。

英文:
import java.util.Scanner;

class piglatin{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println(&quot;enter a string&quot;);
        String s=in.nextLine(),n1=&quot;&quot;,n2=&quot;&quot;,n3=&quot;&quot;;
        int i;
        for(i=0;i&lt;s.length();i++){
            if(s.charAt(i)==&#39;a&#39;||s.charAt(i)==&#39;e&#39;||s.charAt(i)==&#39;o&#39;||s.charAt(i)==&#39;i&#39;||s.charAt(i)==&#39;u&#39;)
                while (s.charAt(i)!=&#39; &#39;&amp;&amp; i!=s.length()){     // hre lies the problem
                    n1+=s.charAt(i);
                    i++;
                }
            else if(s.charAt(i)==&#39; &#39;){
                n2=&quot;&quot;;
                i++;
            }
            else
                n2+=s.charAt(i);

            n3=n3+n1+n2+&quot;ay &quot;;


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

here when i matches the value of length the loop should break but it always show outofbounds exception. what is my mistake i have used && and the gateway should give false && true = false
please help

答案1

得分: 0

在您的最后一次迭代中,while (s.charAt(i)!=&#39; &#39;&amp;&amp; i!=s.length()){,它试图获取s.charAt(i),导致IndexOutOfBounds,因为i = length,意味着它对于数组的大小来说过高了一个值。

如果您先检查大小,它会首先求值为不真,因为i = length,所以永远不会到达s.charAt(i),因为第一次求值不会为真,从而永远不会抛出异常。这称为短路。

英文:

On your last iteration while (s.charAt(i)!=&#39; &#39;&amp;&amp; i!=s.length()){

It tries to grab s.charAt(i) causing an IndexOutOfBounds, because i = length meaning that is one value to high for the size of the array.

If you check the size first it will evaluate to be not true because i = length so you will never reach the s.charAt(i) because the first evaluation will not be true, thus never throwing the exception. This is called short circuiting.

答案2

得分: 0

定义你条件的保险丝是错误的!

if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || 
    s.charAt(i) == 'o' || s.charAt(i) == 'i' || s.charAt(i) == 'u')
           
    while (s.charAt(i) != ' ' && i != s.length()) {     // 这里出现了问题
        n1 += s.charAt(i);
        i++;
    }

如果字符是 a、e、i、o 或 u,为什么要检查 s.charAt(i) != ' '
另一方面,
如果你像这样使用 for 循环

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

在这种情况下,i 永远不会达到 s.length,为什么要执行 i != s.length()

那个 while 循环的条件实际上是无效代码。

英文:

the fuses defining your condition are wrong!

if(s.charAt(i)==&#39;a&#39;|| s.charAt(i)==&#39;e&#39;|| 
   s.charAt(i)==&#39;o&#39;|| s.charAt(i)==&#39;i&#39;|| s.charAt(i)==&#39;u&#39;)
       
    while (s.charAt(i) != &#39; &#39; &amp;&amp; i!=s.length()){     // hre lies the problem
        n1+=s.charAt(i);
        i++;
    }

if the char is a or e, i, o or u then why check s.charAt(i) != &#39; &#39;??
on the other hand,
if you do a for like this

for(i=0;i&lt;s.length();i++){ 

where i will never reach the s.length why do u do: i!=s.length()

that while condition is just dead code

答案3

得分: 0

Change your while condition to this:

while (i != s.length() && s.charAt(i) != ' ')

You should check the length first before using charAt(i).

英文:

Change your while condition to this :

while (i != s.length() &amp;&amp; s.charAt(i) !=&#39; &#39;)

You should check the length first before charAt(i)

huangapple
  • 本文由 发表于 2020年8月26日 01:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63584121.html
匿名

发表评论

匿名网友

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

确定