如何使用课后清除字符串?

发布于 2025-02-12 18:56:04 字数 1456 浏览 1 评论 0原文

这是我正在研究的一种回文算法。

import java.util.*;
import java.io.*;
    
public class Palindrome {
    String reverse = "";

    Palindrome() {}


    String reverseStr(String inputs) {
        for (int i = inputs.length() - 1; i >= 0; i--) {
            reverse += inputs.charAt(i);
        }
        return reverse;
    }

    public static void main(String[] args) {
        Palindrome sinput = new Palindrome();
        int choice = 1;
        Scanner scan = new Scanner(System.in);
        while (choice <= 1) {

            System.out.println("Print new word");
            String inputs = scan.nextLine();

            String reversed = sinput.reverseStr(inputs);
            System.out.println(reversed);
            //   reversed.clear();   Doesn't work creates Complier error
            //  reversed.nextLine();  Doesn't work creates Complier error
            scan.reset(); // Doesn't work 
            //reversed.fill(reversed,0); Doesn't work 
            System.out.println("To Continue with new word Enter 1 ; to quit Enter One");

            String choiceString = scan.nextLine();
            choice = Integer.parseInt(choiceString);

            if (choice == 1) {
                continue;

            } else if (choice > 1) {
                break;
            }
        }

        scan.close();
    }
}

它仅在一个例外工作。它没有清除字符串,而是附加它。我尝试了几个例子。我尝试的那些被评论了。我仍然想将palindrome课程保留为单独的课程。在循环运行时,清除反向或屏幕屏幕的最佳方法是什么?

This is a palindrome algorithm that I'm working on.

import java.util.*;
import java.io.*;
    
public class Palindrome {
    String reverse = "";

    Palindrome() {}


    String reverseStr(String inputs) {
        for (int i = inputs.length() - 1; i >= 0; i--) {
            reverse += inputs.charAt(i);
        }
        return reverse;
    }

    public static void main(String[] args) {
        Palindrome sinput = new Palindrome();
        int choice = 1;
        Scanner scan = new Scanner(System.in);
        while (choice <= 1) {

            System.out.println("Print new word");
            String inputs = scan.nextLine();

            String reversed = sinput.reverseStr(inputs);
            System.out.println(reversed);
            //   reversed.clear();   Doesn't work creates Complier error
            //  reversed.nextLine();  Doesn't work creates Complier error
            scan.reset(); // Doesn't work 
            //reversed.fill(reversed,0); Doesn't work 
            System.out.println("To Continue with new word Enter 1 ; to quit Enter One");

            String choiceString = scan.nextLine();
            choice = Integer.parseInt(choiceString);

            if (choice == 1) {
                continue;

            } else if (choice > 1) {
                break;
            }
        }

        scan.close();
    }
}

It works only with one exception. Instead of clearing out a string, it appends it. I tried a couple of examples. The ones that I tried are commented out. I still want to keep the palindrome class as a separate class. What would be the best way to clear reverse or sinput screen each time while loop runs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情丝乱 2025-02-19 18:56:04

您可以通过将反向移动到该方法中来更新reversEstr方法来解决此问题,该方法每次调用ReversEstr都会有效地都有一个新的字符串,否则您可以使用反向=“”;在“重置”它的方法之前。这是一个工作解决方案:

String reverseStr(String inputs) {
    //Move the reverse variable inside you mothed so that it gets reset every time reverseStr is called
    String reverse=""; 

    //Now do the loop
    for (int i =inputs.length()-1; i>=0; i-- ) {
        reverse+=inputs.charAt(i);      
    }
    return reverse;
}

You can fix this by updating your reverseStr method by moving reverse into the method which will effectively have a fresh string every time you call reverseStr, otherwise you can use reverse = ""; before the method to "reset" it. Here is a working solution:

String reverseStr(String inputs) {
    //Move the reverse variable inside you mothed so that it gets reset every time reverseStr is called
    String reverse=""; 

    //Now do the loop
    for (int i =inputs.length()-1; i>=0; i-- ) {
        reverse+=inputs.charAt(i);      
    }
    return reverse;
}
放飞的风筝 2025-02-19 18:56:04

而不是通过字符串字符迭代并重新发明轮子:

只是使用Java已经内置的东西?

“为什么不 复杂并给您进一步的问题。

    String reverseStr(String inputs) {
      return new StringBuilder(inputs).reverse().toString();
    }

编辑:

    // Add the import before the class:
    import java.lang.StringBuilder;

上次编辑:

这已经在JSHELL上进行了测试,并与JDK 一起进行了测试

    jshell> import java.lang.StringBuilder;

    jshell> System.out.println(new StringBuilder("Testing input").reverse().toString());

    tupni gnitseT 

Instead of iterating through a string character-by-character and reinventing the wheel:

"Why not just use what Java already has built-in?"

Also, you don't need a variable which complicates and gives you further problems.

    String reverseStr(String inputs) {
      return new StringBuilder(inputs).reverse().toString();
    }

Edit:

    // Add the import before the class:
    import java.lang.StringBuilder;

Last Edit:

This has already been tested on JShell, built in with the JDK

    jshell> import java.lang.StringBuilder;

    jshell> System.out.println(new StringBuilder("Testing input").reverse().toString());

    tupni gnitseT 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文