巧用string中的contains巧解一道题目

Posted on 2019-03-09 17:48  ANONY_MOUSER  阅读(175)  评论(0)    收藏  举报
题目:求0—7所能组成的奇数个数。假设最高八位数字.
 1 package edu.yuliang.lianxiti50;
 2 /*
 3 题目:求0—7所能组成的奇数个数。
 4 *程序分析:最少也是1位数,最多能组成8位的数字
 5  *       第一位不能为零,最后一位不能是偶数
 6  *       0到7有四个奇数
 7  *       这里用累加求和
 8  */
 9 public class p43 {
10     public static void main(String[] args) {
11           int count = 0;
12 //        int a=12345967;
13 //        String string1= String.valueOf(a);
14 //        System.out.println(string1);
15 //        System.out.println(string1.contains("8|9"));
16         for (double i = 0; i < 100000000; i++) {
17             String string = String.valueOf(i);
18             if (string.contains("8") ||string.contains("9")) {
19                 continue;
20             }
21             if (i % 2 == 1) {
22                 count++;
23             }
24         }
25         System.out.println("0-7可以组成:" + count);
26     }
27 
28 
29     /**
30      * 判断字符串中是否有重复字母
31      */
32 
33     public static boolean checkDifferent(String iniString){
34         boolean isbool = false;
35         char[] chars = iniString.toCharArray();
36         for (int i = 0; i < chars.length; i++) {
37             for (int j = i+1; j < chars.length; j++) {
38                 if(chars[i] == chars[j]){
39                     isbool = true;
40                     return isbool;
41                 }else {
42                     isbool = false;
43                 }
44             }
45         }
46         return isbool;
47     }
48 
49 }