简体   繁体   English

我的程序没有打印没有'g'和/或'n'的字符串。 为什么会这样呢?

[英]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.

相关问题 为什么我的程序打印“ null”而不是字符串? - Why is my program printing “null” instead of the string? 为什么我的程序打印值没有相应的打印语句? - Why is my program printing values without a corresponding print statement? 为什么我的程序关闭而不打印堆栈跟踪或异常? - Why does my program close without printing a stack trace, or exception? 当变量是字符串时,为什么程序会打印数字? - Why is the program printing numbers when the variable is a string? 为什么我的程序在输入后没有打印出来? - Why is my program not printing out after the input? 当我通过方法将它作为输入时,为什么我的 java 程序在 output 中为字符串打印“null”? - why is my java program printing "null" in output for a string when I took it as an input through a method? 为什么时间复杂度是 n*n*n! 对于下面的算法打印字符串的所有排列? - why the time complexity is n*n*n! for the below algorithm printing all the permutation of a string? 我的服务器和客户端套接字程序只是挂起而没有打印任何内容 - My Server and Client socket program just hangs without printing anything 为什么我的Java程序每次都打印零? - Why is my java program printing zero every time? 为什么我的程序不打印我想要的内容? - Why isnt my program printing out what i want it to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM