1.、随便输入一段数字字符串,把其中连续出现次数最多的字符找出来并统计出连续出现的字符此次数, 如:233111333。
public class Zifuchuan { public static void main(String[]args){ char ch=' '; String str="2331113333"; int times=0; int time=1; for(int i=0;i<str.length()-1;i++){ if(str.charAt(i)==str.charAt(i+1)) time++; else { if(time>times){ times=time; ch=str.charAt(i); }time=1; } } if(time>times){ times=time; ch=str.charAt(str.length()-1); } System.out.println(ch+" "+times); } }
2.、有如下字符串“iu7i8csr83sdf9",将其中的数字摘取出来组成一个 int 的数值输出。
public class ZI { public static void main(String[] args) { String s = "iu7i8csr83sdf9"; char[] c = s.toCharArray(); int[] is = new int[c.length]; //定义int类型数组存储数字 int count = 0; for (char c1 : c) { if (c1 >= 48 && c1 <= 58) { String s1 = String.valueOf(c1); //将字符转为字符串 is[count] = Integer.parseInt(s1); //将字符串转为int数字 count++; } } for (int i = 0; i < count; i++) { System.out.print(is[i]); } } }