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);
}
}
}
}