英文:
!= 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("enter a string");
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()){ // hre lies the problem
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);
}
}
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)!=' '&& i!=s.length()){
,它试图获取s.charAt(i)
,导致IndexOutOfBounds
,因为i = length
,意味着它对于数组的大小来说过高了一个值。
如果您先检查大小,它会首先求值为不真,因为i = length
,所以永远不会到达s.charAt(i)
,因为第一次求值不会为真,从而永远不会抛出异常。这称为短路。
英文:
On your last iteration while (s.charAt(i)!=' '&& 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)=='a'|| s.charAt(i)=='e'||
s.charAt(i)=='o'|| s.charAt(i)=='i'|| s.charAt(i)=='u')
while (s.charAt(i) != ' ' && 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) != ' '
??
on the other hand,
if you do a for like this
for(i=0;i<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() && s.charAt(i) !=' ')
You should check the length first before charAt(i)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论