回 帖 发 新 帖 刷新版面

主题:一条测试题,请指教!

定义一个长度为5的字符串数组,并初始化其初值为"open","door","the","open","name";
计算该数组中"open"出现的次数,并倒序输出数组元素.

public class Teat{
  public void static main(String[] args){
   [u](1) [/u]//声明并初始化数组
    int count=0;
   [u](2)[/u] //计算该数组中"open"出现的次数
      {
         [b](3)[/b]
         count++;       
      }
   [u] (4)[/u] //倒序输出数组元素
      {
       [u](5)[/u]      
      }
   }
}

请在以上(1)-(5)出填进代码...

回复列表 (共1个回复)

沙发

public class Teat
{
  public static void main(String[] args)
  {
           String [] str = new String[]{"open","door","the","open","name"};
           int count=0;
          for(int i=0;i<str.length;i++)
            if(str[i].equals("open"))
                 count++;       
        System.out.println (count);
           for(int i=str.length-1;i>=0;i--)
           {
               System.out.println (str[i]); 
           }
   }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册