英文:
Why character in StringBuild does not change?
问题
字符必须从控制台输入,以在此行更改为小写字母。但它显示相同的单词,符号不会改变。
public class Task {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(requestString());
char symbol = requestSymbol().charAt(0);
int count = 0;
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == symbol) {
sb.setCharAt(i, Character.toLowerCase(sb.charAt(i)));
count++;
}
}
System.out.println("Number of entries: " + count);
System.out.println("Converted string: " + sb);
}
static String requestString() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter string:");
return scanner.nextLine();
}
static String requestSymbol() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the symbol:");
return scanner.next();
}
}
英文:
The character must be entered from the console to change to lowercase letters on this line. But it displays the same word and the symbol does not change.
public class Task {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(requestString());
char symbol = requestSymbol().charAt(0);
int count = 0;
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == symbol) {
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
count++;
}
}
System.out.println("Number of entries: " + count);
System.out.println("Converted string: " + sb);
}
static String requestString() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter string:");
return scanner.nextLine();
}
static String requestSymbol() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the symbol:");
return scanner.next();
}
}
答案1
得分: 3
似乎是这一行出了问题:
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
应该改为:
sb.setCharAt(i, Character.toUpperCase(symbol));
英文:
It seems to be a problem with the line:
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
It should be:
sb.setCharAt(i, Character.toUpperCase(symbol));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论