本帖最后由 RyanBern 于 2015-8-21 09:54 编辑
瞎写玩玩,长时间不用Java,不太熟练。
class Student{ private String stuNo = ""; private String stuName = ""; public Student(String no, String name){ this.stuNo = no; this.stuName = name; } public String getStuNo(){ return stuNo; } public void setStuNo(stuNo){ this.stuNo = stuNo; } public String getStuName(){ return stuName; } public void setStuName(name){ this.stuName = name; } public String toString(){ return "姓名:" + stuName + " " + "学号:" + stuNo; } public boolean equals(Object obj){ if(!(obj instanceof String)) return false; Student stu = (Student)obj; return stuNo.equals(stu.getStuNo()); } }
class Student{
private String stuNo = "";
private String stuName = "";
public Student(String no, String name){
this.stuNo = no;
this.stuName = name;
}
public String getStuNo(){
return stuNo;
}
public void setStuNo(stuNo){
this.stuNo = stuNo;
}
public String getStuName(){
return stuName;
}
public void setStuName(name){
this.stuName = name;
}
public String toString(){
return "姓名:" + stuName + " " + "学号:" + stuNo;
}
public boolean equals(Object obj){
if(!(obj instanceof String)) return false;
Student stu = (Student)obj;
return stuNo.equals(stu.getStuNo());
}
}
import java.io.*; public class DateConvert{ public static void main(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readline(); int first_index = input.indexOf('/'); int last_index = input.lastIndexOf('/'); if(first_index < 0 || last_index == first_index){ System.out.println("无效的日期格式"); return; } String month = input.substring(0, first_index - 1); String day = input.substring(first_index + 1, last_index - 1); String year = input.substring(last_index + 1); System.out.println(year + "年" + month + "月" + day + "日"); } }
import java.io.*;
public class DateConvert{
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readline();
int first_index = input.indexOf('/');
int last_index = input.lastIndexOf('/');
if(first_index < 0 || last_index == first_index){
System.out.println("无效的日期格式");
return;
}
String month = input.substring(0, first_index - 1);
String day = input.substring(first_index + 1, last_index - 1);
String year = input.substring(last_index + 1);
System.out.println(year + "年" + month + "月" + day + "日");
}
}
只写了方法,main方法调用过程自己补上。
public class StringCounter{ public static void main(String[] args){} public static void countLettersAndNumbers(String str){ int letter_count = 0, number_count = 0; char ch = NULL; for(int i = 0;i < str.length();i++){ ch = str.charAt(i); if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') letter_count += 1; if(ch >= '0' && ch <= '9') number_count += 1; } System.out.println("字母数:" + letter_count); System.out.println("数字数:" + number_count); } }
public class StringCounter{
public static void main(String[] args){}
public static void countLettersAndNumbers(String str){
int letter_count = 0, number_count = 0;
char ch = NULL;
for(int i = 0;i < str.length();i++){
ch = str.charAt(i);
if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') letter_count += 1;
if(ch >= '0' && ch <= '9') number_count += 1;
}
System.out.println("字母数:" + letter_count);
System.out.println("数字数:" + number_count);
}
}
这个能稍微简单点,但是效率差,有效率高的版本这里没写:
String str = "测试字符串"; int counts = str.split("朋友").length();
String str = "测试字符串";
int counts = str.split("朋友").length();
借助正则表达式:
//需要import java.util.regex; String str = "测试字符串"; String output = str; String[] words_to_filter = {"敏感词1", "敏感词2"}; for(int i = 0;i < words_to_filter.length();i++){ String word = words_to_filter[i]; output = output.replaceAll(word, "**"); }
//需要import java.util.regex;
String str = "测试字符串";
String output = str;
String[] words_to_filter = {"敏感词1", "敏感词2"};
for(int i = 0;i < words_to_filter.length();i++){
String word = words_to_filter[i];
output = output.replaceAll(word, "**");
}
|