如何使用for循环解决以下程序,以生成适当的输出?

huangapple go评论78阅读模式
英文:

How to resolve the following program with a for loop into producing an appropriate output?

问题

以下是翻译好的部分:

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 = "";

        // 替换最后一个字符
        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; // 记录字符出现的次数
        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);
    }
}
英文:

The following Java program is supposed to manipulate a string input by the user in such a way that the user will decide which character needs to be replaced with another and just the last character from the string should be replaced. Example if the user enters the string "OYOVESTER" and decides to replace "O" with "L", the program should output the following result: "OYLVESTER" (notice that only the last "O" was replaced with "L")

NOTE: YOU CANNOT USE BREAK COMMAND TO STOP THE LOOP. IT IS PROHIBITED.

import java.util.Scanner;
public class StringFun {

	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println(&quot;Enter the string to be manipulated&quot;);
		String inString = keyboard.nextLine();
		String outString = &quot;&quot;;
		
		//Replace Last
		System.out.println(&quot;Enter the character to replace&quot;);
		char oldCharF = keyboard.next().charAt(0);
		
		System.out.println(&quot;Enter the new character&quot;);
		char newCharF = keyboard.next().charAt(0);
		
		int count = 0; // variable that tracks number of letter occurrences
		for(int index = inString.length() - 1;index &gt;= 0;index--) {
			if(inString.charAt(index) == oldCharF &amp;&amp; count &lt; 1){
				outString = newCharF + outString;
				outString = outString + inString.substring(0,index);
				count++;
				
			}
			if (count &lt; 1) {
				outString = outString + inString.charAt(index);
			}
			
		}

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

	}

}

I keep getting the following output which is incorrect:

Enter the string to be manipulated

OYOVESTER

Enter the character to replace

O

Enter the new character

L

The new sentence is: LRETSEVOY

答案1

得分: 1

有许多更简单的方法可以实现您的要求,但我希望您能用循环(无需使用break语句)来演示这一点。

然后您可以使用类似这样的代码:

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;
  }
}

附注:在循环内部使用字符串拼接不推荐,因为每次字符串拼接都会复制整个字符串,通常最好将其替换为对StringBuilder.append()StringBuffer.append()的显式调用。

英文:

There are many simpler ways to achieve your requirement but I hope you have to demonstrate this with loops (without breaks)

Then you can use some thing like this :

boolean skip = false;

for (int index = inString.length() - 1; index &gt;= 0; index--) {
  if (!skip &amp;&amp; inString.charAt(index) == oldCharF) {
    outString = newCharF + outString;
    skip = true;
  }
  else {
    outString = inString.charAt(index) + outString;
  }
}

PS : Using String concatenation inside loops is not recommended since
every String concatenation copies the whole String, usually it is preferable to
replace it with explicit calls to StringBuilder.append() or StringBuffer.append()

答案2

得分: 0

以下是翻译好的代码部分:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("输入要操作的字符串");
    String word = keyboard.nextLine();

    // 替换最后一个字符
    System.out.println("输入要替换的字符");
    char oldCharF = keyboard.next().charAt(0);

    System.out.println("输入新字符");
    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("新句子为:" + word);
}
英文:

No break command seems like a weird condition. You could just a boolean value, and other methods, to break the loop when you need. Why not do something like this?

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println(&quot;Enter the string to be manipulated&quot;);
    String word = keyboard.nextLine();

    //Replace Last
    System.out.println(&quot;Enter the character to replace&quot;);
    char oldCharF = keyboard.next().charAt(0);

    System.out.println(&quot;Enter the new character&quot;);
    char newCharF = keyboard.next().charAt(0);

    int index = word.lastIndexOf(oldCharF);
    if(index &gt; 1){
        word = word.substring(0,index) + newCharF + word.substring(index+1);
    }

    System.out.println(&quot;The new sentence is: &quot; + word);
}

huangapple
  • 本文由 发表于 2020年10月15日 19:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64370946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定