找一段话中有几个字母A/a。
在 if ( find == "a" || find == "A" ) 这里报错。package Search;public class Search_1
{
static String Text = "For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life. For God did not send his Son into the world to condemn the world, but to save the world through him. Whoever believes in him is not condemned, but whoever does not believe stands condemned already because he has not believed in the name of God's one and only Son. This is the verdict: Light has come into the world, but men loved darkness instead of light because their deeds were evil. Everyone who does evil hates the light, and will not come into the light for fear that his deeds will be exposed. But whoever lives by the truth comes into the light, so that it may be seen plainly that what he has done has been done through God.";

public static void main ( String [ ] args )
{
int Na = 0;
for ( int i = 0 ; i < Text.length ( ) ; i ++ )
{
char find = Text.charAt ( i );
 if ( find == "a" || find == "A" ) { Na ++ ; }
}
System.out.println ( "a/A有" + Na + "个" );
}
}

解决方案 »

  1.   

    if ( find == 'a' || find == 'A' )
      

  2.   

    字符不能跟字符串比较,if ( find == "a" || find == "A" )改成 if ( find == 'a' || find == 'A' )
    public class Search_1
    {
        static String Text = "For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life. For God did not send his Son into the world to condemn the world, but to save the world through him. Whoever believes in him is not condemned, but whoever does not believe stands condemned already because he has not believed in the name of God's one and only Son. This is the verdict: Light has come into the world, but men loved darkness instead of light because their deeds were evil. Everyone who does evil hates the light, and will not come into the light for fear that his deeds will be exposed. But whoever lives by the truth comes into the light, so that it may be seen plainly that what he has done has been done through God.";
        
        public static void main ( String [ ] args )
        {
            int Na = 0;
            for ( int i = 0 ; i < Text.length ( ) ; i ++ )
            {
                char find = Text.charAt ( i );
                 if ( find == 'a' || find == 'A' ) { Na ++ ; }
            }
            System.out.println ( "a/A有" + Na + "个" );        
        }
    }