如何使用课后清除字符串?
这是我正在研究的一种回文算法。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过将
反向
移动到该方法中来更新reversEstr
方法来解决此问题,该方法每次调用ReversEstr都会有效地都有一个新的字符串,否则您可以使用反向=“”;
在“重置”它的方法之前。这是一个工作解决方案:You can fix this by updating your
reverseStr
method by movingreverse
into the method which will effectively have a fresh string every time you call reverseStr, otherwise you can usereverse = "";
before the method to "reset" it. Here is a working solution:而不是通过字符串字符迭代并重新发明轮子:
只是使用Java已经内置的东西?
“为什么不 复杂并给您进一步的问题。
编辑:
上次编辑:
这已经在JSHELL上进行了测试,并与JDK 一起进行了测试
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.
Edit:
Last Edit:
This has already been tested on JShell, built in with the JDK