如何在不使用 break 函数的情况下结束下面的循环?

How to end the following loop without using the break function?

下面的 Java 程序应该以用户决定哪个字符需要替换为另一个字符的方式来操作用户输入的字符串,并且应该只替换字符串中的第一个字符更换。例如,如果用户输入字符串“PUMP”并决定用“L”替换“P”,程序应该输出以下结果:“LUMP”(注意只有第一个“P”被替换为“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 the first character from the string entered by the user only
        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);
        
        
        for(int index = 0;index < inString.length();index++) {
            if(inString.charAt(index) == oldCharF){
                outString = outString + newCharF;
                
            }
            else {
                outString = outString + inString.charAt(index);
            }
            
        }

        System.out.print("The new sentence is: "+outString);
        

    }

}

我一直从上面的代码中得到这个结果:

Enter the string to be manipulated

PUMP

Enter the character to replace

P

Enter the new character

L

The new sentence is: LUML

执行此操作的最简单方法之一是添加一个计数器来跟踪遇到您要更改的字母的次数。因为你要查找第一次出现的字母,并且只想改变一个字母,你可以这样做:

  • 添加一个名为count的整数变量并将其实例化为0。
  • 通过添加“&& counter < 0”更改 for 循环中的 if 条件
  • 在同一个 if 条件的正文中添加“count++”。

这将在第一次遇到正在更改的字母时递增计数变量。因为计数现在等于 1,它会限制任何其他字母被更改,并且会解决您之前遇到的问题。

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 = "";
        int count = 0; // variable that tracks number of letter occurrences
        
        //Replace the first character from the string entered by the user only
        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);
        
        
        for(int index = 0;index < inString.length();index++) 
        {
            if(inString.charAt(index) == oldCharF && count < 1) //Condition filter 
            {
                outString = outString + newCharF;
                count++; //Increment counter
                
            }
            else
                outString = outString + inString.charAt(index);
  
        System.out.print("The new sentence is: "+outString);
    }
}

并不是说您真的想要 break; 在您的代码中,结束您的 for 循环而不使用 break; 语句您需要做的就是是利用一个布尔标志来指示要替换的第一个字符已经找到并处理,例如:

String inString = "PUMP";
char oldCharF = 'P';
char newCharF = 'L';
String outString = "";
    
boolean charFound = false;
for (int index = 0; index < inString.length(); index++) {
    char inChar = inString.charAt(index);
    /* Is the current character (inChar) what we're 
       looking for (oldCharF) AND (&&) is it NOT 
       found yet (!charFound)?       */
    if (inChar == oldCharF && !charFound) {
        // Yes, it's the one we want
        outString += newCharF;  // Append our alternate character to outString.
        charFound = true;       // Change flag to true.
    }
    /* No, it's not the character we want OR 
       the desired character has already been 
       processed.               */
    else {
        outString += inChar;
    }
}

// Display the result held in outString to Console Window...
System.out.println(outString);