java读取键盘输入的任意字符串,如:She is a very good teacher, 按要求执行以下操
答案:2 悬赏:80 手机版
解决时间 2021-11-20 18:22
- 提问者网友:寂寞撕碎了回忆
- 2021-11-20 10:43
java读取键盘输入的任意字符串,如:She is a very good teacher, 按要求执行以下操
最佳答案
- 五星知识达人网友:持酒劝斜阳
- 2021-11-20 12:09
public static void main(String[] args) {
String str = "She is a very good teacher";
String[] strs = str.split("\s+");
String newStr = "";
//把首字母变大写
for (int i = 0; i < strs.length; i++) {
//可以加判断如果一个字母就不大写等等
newStr += strs[i].substring(0, 1).toUpperCase()+strs[i].substring(1)+" ";
}
System.out.println(newStr);
//下面的e和er可以自己从键盘输入
int sum = 0;
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if(ch[i] == 'e'){
sum ++;
}
}
String[] strs2 = str.split("er");
System.out.println("e的个数:"+sum+" er的个数:"+(strs2.length-1));
}
//结果
//She Is A Very Good Teacher
//e的个数:4 er的个数:1
String str = "She is a very good teacher";
String[] strs = str.split("\s+");
String newStr = "";
//把首字母变大写
for (int i = 0; i < strs.length; i++) {
//可以加判断如果一个字母就不大写等等
newStr += strs[i].substring(0, 1).toUpperCase()+strs[i].substring(1)+" ";
}
System.out.println(newStr);
//下面的e和er可以自己从键盘输入
int sum = 0;
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if(ch[i] == 'e'){
sum ++;
}
}
String[] strs2 = str.split("er");
System.out.println("e的个数:"+sum+" er的个数:"+(strs2.length-1));
}
//结果
//She Is A Very Good Teacher
//e的个数:4 er的个数:1
全部回答
- 1楼网友:刀戟声无边
- 2021-11-20 12:58
import java.util.Scanner;
public class StringUnit {
private Scanner input = new Scanner(System.in);
public String getInputString(String tips) {
System.out.print(tips);
return input.nextLine();
}
public int countCharacter(String str, char target) {
if (null != str) {
int count = 0;
for (int index = 0; index < str.length(); index++) {
if (str.charAt(index) == target) {
count++;
}
}
return count;
}
return 0;
}
public int countString(String str, String target) {
if (null == str || null == target || target.length() > str.length()) {
return 0;
}
int count = 0;
for (int index = 0; index < str.length() - target.length(); index++) {
String temp = str.substring(index, index + target.length());
if (temp.equals(target)) {
count++;
}
}
return count;
}
public String getNewString(String str) {
String[] characters = str.split(" ");
String temp = "";
for (int index = 0; null != characters && index < characters.length; index++) {
String subStr = characters[index].substring(0, 1);// 首字母
temp += subStr.toUpperCase()
+ characters[index]
.substring(1, characters[index].length()) + " ";
}
return temp;
}
public static void main(String[] args) {
StringUnit unit = new StringUnit();
String str = unit.getInputString("Input your string: ");
System.out.println("count e char: " + unit.countCharacter(str, 'e'));
System.out.println("count er str: " + unit.countString(str, "er"));
System.out.println("change str: " + unit.getNewString(str));
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯