[英]My program is not printing the string without 'g' and/or 'n'. Why is it so?
Here is my code: 这是我的代码:
public class test
{
public static void main(String y)
{
int l =y.length();
for(int i=0;i<=l-1;i++)
{
if(y.charAt(i)!='g'||y.charAt(i)!='n')
System.out.print(y.charAt(i));
else
continue;
}}
}
I think && would work. 我认为&&可以。 Or operator checks if only one statement is true.
或操作员检查是否只有一个陈述为真。 Therefore, result will always be true and the input string would be same to output string.
因此,结果将始终为true,并且输入字符串与输出字符串相同。
The problem is that you used OR (||) insted of AND (&&) 问题是您使用的是AND(&&)的OR(||)
This code will always return true: 此代码将始终返回true:
(y.charAt(i)!='g' || y.charAt(i)!='n')
Try this instead: 尝试以下方法:
public class test
{
public static void main(String y)
{
int l =y.length();
for(int i=0;i<=l-1;i++)
{
if(y.charAt(i)!='g' && y.charAt(i)!='n')
System.out.print(y.charAt(i));
else continue;
}
}
}
Try this: 尝试这个:
public class test
{
public static void main(String y)
{
int l =y.length();
for(int i=0;i<=l-1;i++)
{
if( y.charAt(i) == 'g'|| y.charAt(i) == 'n'){
continue;
} else {
System.out.print(y.charAt(i));
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.