Java2

1.
{張三:語文,80;數學,90/
李四:語文,90;數學,80/
~
}
求語文的平均成績,數學的平均成績

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2;

/**
 *
 * @author ymh
 */
public class Java2 {

    /**
     * @param args the command line arguments
     */

    public static int Chi1 = 80, Chi2 = 90, Math1 = 90, Math2 = 80;
    public static void main(String[] args) {
        // TODO code application logic here

        System.out.printf("%f %f", (Chi1 + Chi2)/2.0 , (Math1+Math2)/2.0);

    }

}

2.對某些字符串中的特定的進行過濾
用了兩種方法
1.string.replace
2.用正則表達式
注:一定要在頭部加入 import java.util.regex.Matcher; 因爲忘了這一句花了好多時間 ……

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author ymh
 */
public class Java2 {

    /**
     * @param args the command line arguments
     */

    public static int num = 0;

    public static void main(String[] args) {
        // TODO code application logic here
        String str = "你是個大智障,大傻蛋";

        str.replaceAll("智障", "**");

        System.out.println(str.replace("智障", "**").replaceAll("傻蛋", "**"));

        //method2
        Pattern p = Pattern.compile("智障|傻蛋");
        Matcher m = p.matcher(str);
        System.out.println(m.replaceAll("**").trim());

    } 
}

3.找到月的位置,並輸出其位置,原文照樣輸出,只是添加月的位置

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2;

/**
 *
 * @author ymh
 */
public class Java2 {

    /**
     * @param args the command line arguments
     */

    public static int num = 0;

    public static void main(String[] args) {
        // TODO code application logic here
        String str = "牀前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。";
        for(int i = 0; i < str.length(); i++)
        {
            char c = str.charAt(i);
            if(c == '月')
            {
                num++;
                System.out.printf("月[%d]", i);
            }
            else
            {
                System.out.print(c);
            }
        }  
    }    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章