| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1064 人关注过本帖
标题:求大神帮忙!编写程序对该数组排序,并输出所有包含“王”字的字符串
取消只看楼主 加入收藏
选调生
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:82
专家分:100
注 册:2016-6-24
结帖率:66.67%
收藏
已结贴  问题点数:18 回复次数:0 
求大神帮忙!编写程序对该数组排序,并输出所有包含“王”字的字符串
但是我的运行运行结果中排序部分的结果不对,是:张合,小王,天王盖地虎,小赵,王敏
但是正确的不是应该是张合,小赵,小王,王敏,天王盖地虎  吗?
class string{
     String [] name;
     public string(String [] name){
           this.name = name;
     }
     void contain_wang(String [] name){
           System.out.println("输出带王的字符串:");
               for(int i=0;i<name.length;i++){
                   //对字符进行分割比较
                       for(int j=0;j<name[i].length();j++){
                              if(name[i].charAt(j)=='王'){
                                      System.out.println(name[i]+' ');
                                  }
                           }
                   }
               //    System.out.println();
           }
        void paixu(String [] name){
                  String name1;
                  System.out.println("字符串数组排序后的结果是");
                  //进行冒泡排序
                  for(int i=0;i<name.length;i++){
                          for(int j=i;j<name.length-1;j++){
                                  if(name[j].compareTo(name[j+1])>=0){   
                                      name1 = name[j];
                                      name[j] = name[j+1];
                                      name[j+1] = name1;
                      }                     
                    }
                      }
               }
           void output(String [] name){
                   for(int i=0;i<name.length;i++){
                         System.out.println(name[i]+' ');
                       }
                   //    System.out.println(' ');
               }
       public static void main(String [] agrs){
                String [] name1 = {"王敏","张合","小王","小赵","天王盖地虎"};
                string name = new string(name1);
                name.contain_wang(name1);
                name.paixu(name1);
                name.output(name1);
               
           }
      
}
2017-04-13 13:57
快速回复:求大神帮忙!编写程序对该数组排序,并输出所有包含“王”字的字符串
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018140 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved