为你一道java题呗 明天就要了 如果有时间帮我看一下呗 感激不尽
发布网友
发布时间:2023-11-19 00:48
我来回答
共1个回答
热心网友
时间:2024-12-14 21:11
程序如下,应该是正确的,没有考虑可扩展性优化什么的
====================================================
package cn.;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class JiuZheng {
public static void main(String args[]) throws Exception {
System.out.println("输入英文句子(回车换行,连续回车退出):");
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = "";
StringBuilder sb = new StringBuilder();
int count = 1;
do {
str = bf.readLine();
if (str.length() == 0) { // 如果输入的字符串为空,则说明只输入了一个回车
System.out.println("程序退出!");
} else {
sb.append(str).append("\r");
count++;
}
} while (str.length() != 0 && count <= 100);
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想替换成大写的英文单词:回车结束");
String tStr = sc.next();
String arr[] = sb.toString().split("\r");
String newArr[] = new String[arr.length];
for(int i = 0; i < arr.length; i++){
arr[i] = Character.toUpperCase(arr[i].charAt(0)) + arr[i].substring(1,arr[i].length());
if(arr[i].indexOf(".")+1 != arr[i].length() && arr[i].indexOf("?")+1 != arr[i].length() && arr[i].indexOf("!")+1 != arr[i].length()){
newArr[i] = (i+1) + ". " + arr[i] + ".";
}else{
newArr[i] = (i+1) + ". " + arr[i];
}
}
sb = new StringBuilder();
for(String s : newArr){
sb.append(s).append("\r");
}
System.out.println(tiHuan(sb.toString(), tStr, new StringBuilder()));
}
private static String tiHuan(String str, String isStr, StringBuilder sb){
if(str.indexOf(isStr) >= 0){
sb.append(str.substring(0, str.indexOf(isStr)));
for(int i = 0; i < isStr.length(); i++){
sb.append(Character.toUpperCase(isStr.charAt(i)));
}
return tiHuan(str.substring(str.indexOf(isStr) + isStr.length()), isStr, sb);
}else{
return sb.toString() + str;
}
}
}
=========================测试数据=============================
输入英文句子(回车换行,连续回车退出):
i am finding my bag.
How are you?
fine, thank you, and you?
i am fine too
have a try
程序退出!
请输入你想替换成大写的英文单词:回车结束
are
1. I am finding my bag.
2. How ARE you?
3. Fine, thank you, and you?
4. I am fine too.
5. Have a try.