一下是我写的,应该能实现你的功能,才疏学浅,可能比较麻烦,
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据,以end代表结束!");
String str1 = ""; //用来装录入的数据
while (true) { //录入字符串无限制,以end结束
str1 += sc.nextLine();
if (str1.lastIndexOf("end") != -1) {
str1 = str1.substring(0, str1.length()-3); //去除end
break;
}
}
String str2 = ""; //用来装改变后的数据
int num = 0; //用来统计字母数
for (int i = 0; i < str1.length(); i++) {
char c = str1.charAt(i);
if (c >= 'a' && c <= 'z') {
str2 += Character.toString(c).toUpperCase();
num++;
} else if (c >= 'A' && c <= 'Z') {
str2 += Character.toString(c).toLowerCase();
num++;
} else {
str2 += "*";
}
}
System.out.println("输入的数据经过转换后为 " + str2 + " 其中包含字母 " + num + " 个。");
//题目2
String s1 = "asdfasjklasioasn", s2 = "as";
Object[] ob = deleteSubString(s1,s2);
System.out.println("从 " + s1 + " 中剔除 " + s2 + " 后变为 " + ob[0] + " 一共剔除了" + ob[1] + " 个");
}
public static Object[] deleteSubString(String str1, String str2) {
Object[] ob = new Object[2]; //装结果
int num = 0; //计数
while (true) {
int tag = str1.indexOf(str2);
if (tag != -1) { //当在str1中找到str2
str1 = str1.substring(0, tag).concat(str1.substring((tag + str2.length()), str1.length()));
num++;
}else {
break;
}
}
ob[0] = str1;
ob[1] = num;
return ob;
}
} |