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);
            }
        }  
    }    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值