如何使用 for 循环解决以下程序以产生适当的输出?
How to resolve the following program with a for loop into producing an appropriate output?
下面的 Java 程序应该以这样的方式操作用户输入的字符串,即用户将决定哪个字符需要替换为另一个字符,并且应该只替换字符串中的最后一个字符更换。例如,如果用户输入字符串“OYOVESTER”并决定用“L”替换“O”,程序应输出以下结果:“OYLVESTER”(注意只有最后一个“O”被替换为“L”)
注意:您不能使用 Break 命令来停止循环。这是禁止的。
import java.util.Scanner;
public class StringFun {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the string to be manipulated");
String inString = keyboard.nextLine();
String outString = "";
//Replace Last
System.out.println("Enter the character to replace");
char oldCharF = keyboard.next().charAt(0);
System.out.println("Enter the new character");
char newCharF = keyboard.next().charAt(0);
int count = 0; // variable that tracks number of letter occurrences
for(int index = inString.length() - 1;index >= 0;index--) {
if(inString.charAt(index) == oldCharF && count < 1){
outString = newCharF + outString;
outString = outString + inString.substring(0,index);
count++;
}
if (count < 1) {
outString = outString + inString.charAt(index);
}
}
System.out.print("The new sentence is: "+outString);
}
}
我不断收到以下不正确的输出:
输入要操作的字符串
卵母
输入要替换的字符
O
输入新字符
L
新句子是:LRETSEVOY
没有中断命令似乎是一种奇怪的情况。您可以只是一个布尔值和其他方法,以在需要时打破循环。为什么不做这样的事情?
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the string to be manipulated");
String word = keyboard.nextLine();
//Replace Last
System.out.println("Enter the character to replace");
char oldCharF = keyboard.next().charAt(0);
System.out.println("Enter the new character");
char newCharF = keyboard.next().charAt(0);
int index = word.lastIndexOf(oldCharF);
if(index > 1){
word = word.substring(0,index) + newCharF + word.substring(index+1);
}
System.out.println("The new sentence is: " + word);
}
有许多更简单的方法可以实现您的要求,但我希望您必须用循环(不间断)来证明这一点
然后你可以使用这样的东西:
boolean skip = false;
for (int index = inString.length() - 1; index >= 0; index--) {
if (!skip && inString.charAt(index) == oldCharF) {
outString = newCharF + outString;
skip = true;
}
else {
outString = inString.charAt(index) + outString;
}
}
PS :不推荐在循环内使用字符串连接,因为
每个字符串连接都会复制整个字符串,通常最好是
将其替换为对 StringBuilder.append()
或 StringBuffer.append()
的显式调用
下面的 Java 程序应该以这样的方式操作用户输入的字符串,即用户将决定哪个字符需要替换为另一个字符,并且应该只替换字符串中的最后一个字符更换。例如,如果用户输入字符串“OYOVESTER”并决定用“L”替换“O”,程序应输出以下结果:“OYLVESTER”(注意只有最后一个“O”被替换为“L”)
注意:您不能使用 Break 命令来停止循环。这是禁止的。
import java.util.Scanner;
public class StringFun {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the string to be manipulated");
String inString = keyboard.nextLine();
String outString = "";
//Replace Last
System.out.println("Enter the character to replace");
char oldCharF = keyboard.next().charAt(0);
System.out.println("Enter the new character");
char newCharF = keyboard.next().charAt(0);
int count = 0; // variable that tracks number of letter occurrences
for(int index = inString.length() - 1;index >= 0;index--) {
if(inString.charAt(index) == oldCharF && count < 1){
outString = newCharF + outString;
outString = outString + inString.substring(0,index);
count++;
}
if (count < 1) {
outString = outString + inString.charAt(index);
}
}
System.out.print("The new sentence is: "+outString);
}
}
我不断收到以下不正确的输出:
输入要操作的字符串
卵母
输入要替换的字符
O
输入新字符
L
新句子是:LRETSEVOY
没有中断命令似乎是一种奇怪的情况。您可以只是一个布尔值和其他方法,以在需要时打破循环。为什么不做这样的事情?
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the string to be manipulated");
String word = keyboard.nextLine();
//Replace Last
System.out.println("Enter the character to replace");
char oldCharF = keyboard.next().charAt(0);
System.out.println("Enter the new character");
char newCharF = keyboard.next().charAt(0);
int index = word.lastIndexOf(oldCharF);
if(index > 1){
word = word.substring(0,index) + newCharF + word.substring(index+1);
}
System.out.println("The new sentence is: " + word);
}
有许多更简单的方法可以实现您的要求,但我希望您必须用循环(不间断)来证明这一点
然后你可以使用这样的东西:
boolean skip = false;
for (int index = inString.length() - 1; index >= 0; index--) {
if (!skip && inString.charAt(index) == oldCharF) {
outString = newCharF + outString;
skip = true;
}
else {
outString = inString.charAt(index) + outString;
}
}
PS :不推荐在循环内使用字符串连接,因为
每个字符串连接都会复制整个字符串,通常最好是
将其替换为对 StringBuilder.append()
或 StringBuffer.append()