java吧 关注:1,237,232贴子:12,706,095
  • 11回复贴,共1

新手求助,这里怎么返回-1

只看楼主收藏回复

新手求助,这里怎么返回-1



来自Android客户端1楼2017-09-12 13:04回复
    .


    来自Android客户端2楼2017-09-12 13:06
    回复
      楼主用了个笨方法解决了 就是再加个if判断 count==count++ 就输出负一 不等于就输出count++


      来自Android客户端3楼2017-09-12 13:13
      收起回复
        求个简单点的方法


        来自Android客户端4楼2017-09-12 13:13
        回复
          contains


          IP属地:山东来自Android客户端5楼2017-09-12 13:35
          回复
            //题目要的是下标,不是个数
            public class Test {
            public static void main(String args[]) {
            String s = "123456781234568";
            char c = '2';
            System.out.println(test1(c, s));
            System.out.println(test2(c, s));
            System.out.println("count = " + test2(c, s).size());
            }
            static List<Integer> test1(char c, String s) {
            List<Integer> result = new ArrayList<Integer>();
            char[] sc = s.toCharArray();
            for (int i = 0; i < sc.length; i++) {
            if (c == sc[i]) {
            result.add(i);
            }
            }
            return result;
            }
            static List<Integer> test2(char c, String s) {
            List<Integer> result = new ArrayList<Integer>();
            int index = s.indexOf(c);
            while (index != -1) {
            result.add(index);
            index = s.indexOf(c, index + 1);
            }
            return result;
            }
            }


            IP属地:浙江7楼2017-09-12 14:24
            回复
              public class Demo01 {
              static Scanner sc = new Scanner(System.in);
              public static void main(String []args) {
              System.out.println("请输入字符串");
              String s = sc.next();
              System.out.println("请输入字符");
              String s1 = sc.next();
              fan(s,s1);
              }
              public static void fan(String s ,String s1) {
              boolean b = false;
              if (s.contains(s1)){
              b=true;
              }
              if (b) {
              char arr [] = s.toCharArray();
              char arr1[] = s1.toCharArray();
              for (int x = 0;x<arr.length;x++) {
              if (arr[x]==arr1[0]) {
              System.out.println(x+1);}
              }
              }else{
              System.out.println(-1);
              }
              }
              }


              IP属地:山东来自Android客户端8楼2017-09-12 14:32
              回复