1。给你一组字符串如:iu7i8hy4jnb2,让你编程输出里面的数字:7842
2。给你一组字符如{1,3,4,7,2,1,1,5,2},让你输出里面出现次数最多且数值最大的一个,出现几次

解决方案 »

  1.   


    import java.util.*;public class TT
    {
    public static void main(String[] args)
    {
    Scanner in = new Scanner(System.in);
    String s = in.nextLine();
    String[] ss = s.split("[A-Za-z]++");
    String s1 = "";
    for(String str : ss)
    s1 += str;
    System.out.println(s1);
    }
    }
      

  2.   

    import java.util.*;public class AA
    {
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            String a = in.nextLine();
            char[] b = a.split("");
            for(int i =0 ; i<b.length;i++){
                if(b[i]>'0' || b[i]<'9'){
                    System.out.println(b[i]);
                }
             }
        }
    }
      

  3.   

    public static void main(String args[]) {

    // 1。给你一组字符串如:iu7i8hy4jnb2,让你编程输出里面的数字:7842 
    String s = "iu7i8hy4jnb2";
    StringBuffer sb = new StringBuffer();
    for (int i = 0 ; i < s.toCharArray().length; i++) {
    if (Character.isDigit(s.charAt(i))) {
    sb.append(s.charAt(i));
    }
    }
    System.out.println (sb.toString());

    // 2。给你一组字符如{1,3,4,7,2,1,1,5,2},让你输出里面出现次数最多且数值最大的一个,出现几次
    int[] arr = {1,3,4,7,2,1,1,5,2};
    int[][] _arr = new int[10][2];

    for (int i = 0; i < 10; i++) {
    _arr[i][0] = i;
    }
    for (int i = 0; i < arr.length; i++) {
    _arr[arr[i]][1]++;
    }
    int max = 0;
    int index = 0;
    for (int i = 0; i < 10; i++) {
    if (_arr[i][1] > max) {
    max = _arr[i][1];
    index = i;
    }
    }
    System.out.println (_arr[index][0] + " " + _arr[index][1]);
    }
      

  4.   

    Scanner in=new Scanner(System.in);
    String s=in.nextLine();
    String regex="\\D";
    String []num=s.split(regex);
     for(int i=0;i<num.length;i++)
      System.out.println(num[i]);
      

  5.   

    // 2。给你一组字符如{1,3,4,7,2,1,1,5,2},让你输出里面出现次数最多且数值最大的一个,出现几次
            int[] arr = {1,3,4,7,2,1,1,5,2};
            int[][] _arr = new int[10][2];
            
            for (int i = 0; i < 10; i++) {
                _arr[i][0] = i;
            }
            for (int i = 0; i < arr.length; i++) {
                _arr[arr[i]][1]++;
            }
            int max = 0;
            int index = 0;
            for (int i = 0; i < 10; i++) {
                if (_arr[i][1] > max) {
                    max = _arr[i][1];
                    index = i;
                }
            }
            System.out.println (_arr[index][0] + " " + _arr[index][1]);
        }好象有问题 
    _arr[arr[i]][1]++;
    就能把数组的数据放入二维数组了吗?请教下  
      

  6.   

    package c;import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;public class StringTest { /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
    System.out.println("qing shu ru");
    BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
    while(true){
    String line = reader.readLine();
    char[] x=line.toCharArray();
    for(int i=0;i<x.length;i++){
    if(x[i]>='0'&& x[i]<='9'){
    System.out.println(x[i]);
    }
    }
    }

    }}
      

  7.   

    //-第二题
    public static void main(String args[]) {
    int[] arr = { 1, 3, 4, 7, 2, 1, 1, 5, 2 };
    int count=1;
    int max=0;
    for(int i=0;i<arr.length;i++){
    if(arr[i]>max){
    max=arr[i];
    count=1;
    }else if(arr[i]==max){
    count+=1;
    }
    }
    System.out.println(max + "最大,出现 " + count+"次");
    }
      

  8.   

    我也是新手,这是我写的代码,欢迎大家帮忙看看行不行。
                  public static void main(String args[]) {
    int[] arr = { 1, 3, 4, 7, 2, 1, 1, 5, 2, 3, 3};
    int max = 0;
    int count = 0;
    Map<Integer,Integer> map = new HashMap<Integer,Integer>();

    for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr.length; j++) {
    if(arr[j]==arr[i]){
    count = count+1;
    }
    }
    //统计出数组中每个数字出现的次数,放在map中
    map.put(arr[i], count);
    count = 0;
    }
    for(Integer s : map.keySet()){
    //比较map中出现次数最多并且最大的数字
    if(map.get(s) >= count && s > max){
    count = map.get(s);
    max = s;
    }
    System.out.println("map.key=="+s+",map.value=="+map.get(s));
      }
    System.out.println(max + "出现次数最多且最大,出现 " + count + "次");
        }
      

  9.   

    "iu7i8hy4jnb2".replaceAll("[^0-9]","");这个才行。更多的JAVA面试题,Java代码,Java学习资料,请到:JAVA世纪网
    更多的数据库面试题,智力面试题,相声,笑话,请到:老紫竹的家
      

  10.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class RegExp {
    public static void main(String[] args) {
    String str = "iu7i8hy4jnb2";
    String newStr = "";
    Pattern patterns = Pattern.compile("\\d");
    Matcher matchers = patterns.matcher(str);
    while(matchers.find()) {
    newStr += matchers.group();
    }

    System.out.println(newStr);
    }
    }
      

  11.   

    String str="iu7i8hy4jnb2";
    System.out.println(str.replaceAll("[^0-9]));
      

  12.   

    public static void main(String args[]) throws Exception { 
    String str = "iu7i8hy4jnb2"; 
    StringBuffer sb = new StringBuffer(); 
    Matcher matcher = Pattern.compile("\\d").matcher(str); 
    while(matcher.find()){ 
    sb.append(matcher.group()); 

    System.out.println(sb.toString()); 
    } 或者更简单的 
    System.out.println(str.replaceAll("[^0-9]",""));