循环后清除 java 字符串

clearing a java string after loop

我是 Java 的新手,正在为 class 创建一个项目,基本上要求用户输入一个字符串,然后测试该字符串并打印它是否是回文 (向前和向后一样......即妈妈或爸爸或赛车)

我已经得到了可以工作的代码,但是我有一个循环设置来重新运行程序或在最后退出。我的问题是,当您重新运行程序并输入另一个字符串输入时,它会将其添加到原始字符串中。

如何每次重置或删除字符串输入,使其重新开始?

感谢您的帮助!另请注意,可能有更好或更快的方法来完成我在这里所做的事情,但我对 java 的了解有限,而且我才刚刚开始,所以我使用了迄今为止学到的知识。谢谢!!

import java.util.Scanner;
public class Palindrome {
  public static void main(String[] args) {

      String input = ""; // Word entered by user
      String reverse = ""; //Reverse of String input
      String redoAnswer; // answer to rerun program

      int length; //length of word entered by user

      boolean test;
      boolean redo; // boolean to rerun program
      boolean exit; // boolean to validate exit/rerun


      Scanner scan = new Scanner(System.in);

      do {
        redo = true;
        exit = true;

        System.out.println("Please enter a string: ");
        input = scan.nextLine();

        length = input.length();

        for (int i = length - 1; i >= 0; i--)
          reverse = reverse + input.charAt(i);

        if (input.equalsIgnoreCase(reverse)) {
          System.out.println("Yes, this string is a palindrome!");
        } else {

          System.out.println("Sorry, this string is NOT a palindrome!");
        }

        do {
          System.out.println("Please type r to restart or q to quit");
          redoAnswer = scan.nextLine().trim().toLowerCase();
          if (redoAnswer.equals("r")) {
            exit = false;
            redo = true;
            continue;
          } else if (redoAnswer.equals("q")) {
            exit = false;
            redo = false;
            System.out.println("Goodbye!");
            continue;
          } else {
            System.out.println("Sorry, I didn't catch that.");
            continue;
          }
        } while (exit);
      } while (redo);
    } //end main
} //end class

好的,在你们的帮助下弄明白了...还重写了代码,这样您就可以继续输入新字符串或键入 q 退出,而不是最后的重做问题。希望这更干净!

import java.util.Scanner;
public class Palindrome_Test {
  public static void main(String[] args) {
      String input = ""; // Word entered by user
      String reverse = ""; //Reverse of String input

      int length; //length of word entered by user

      boolean redo = true; // boolean to rerun program

      Scanner scan = new Scanner(System.in);

      do {
        System.out.println("Please enter a string, or type Q to quit: ");
        input = scan.nextLine();
        if (input.equalsIgnoreCase("q")) {
          System.out.println("Goodbye!");
          redo = false;
        } else {
          length = input.length();
          for (int i = length - 1; i >= 0; i--)
            reverse = reverse + input.charAt(i);

          if (input.equalsIgnoreCase(reverse)) {
            System.out.println("Yes, this string is a palindrome!");
          } else {
            System.out.println("Sorry, this string is NOT a palindrome!");
          }
          reverse = "";
        }
      } while (redo);
    } //end main
} //end class

在 while 循环的末尾添加 reverse = "";

您会注意到我移动了以下内容 -

String input = ""; // Word entered by user
String reverse = ""; //Reverse of String input

第一个循环内部。尽管您可以在循环结束时简单地重置两个变量...

input = "";
reverse = "";        

没有必要(尽管它们都有效!)。通过处理循环内部变量的范围,每次循环执行时它基本上都会"refresh"。

import java.util.Scanner;

public class  Palindrome {
  public static void main(String[] args) {

   //  String input = ""; // Word entered by user
   //  String reverse = ""; //Reverse of String input
  String redoAnswer; // answer to rerun program

  int length; //length of word entered by user

  boolean test;
  boolean redo; // boolean to rerun program
  boolean exit; // boolean to validate exit/rerun


  Scanner scan = new Scanner(System.in);

  do {
    redo = true;
    exit = true;

    String input = ""; // Word entered by user
    String reverse = ""; //Reverse of String input

    System.out.println("Please enter a string: ");
    input = scan.nextLine();

    length = input.length();

    for (int i = length - 1; i >= 0; i--)
      reverse = reverse + input.charAt(i);

    if (input.equalsIgnoreCase(reverse)) {
      System.out.println("Yes, this string is a palindrome!");
    } else {

      System.out.println("Sorry, this string is NOT a palindrome!");
    }

    do {
      System.out.println("Please type r to restart or q to quit");
      redoAnswer = scan.nextLine().trim().toLowerCase();
      if (redoAnswer.equals("r")) {
        exit = false;
        redo = true;
        continue;
      } else if (redoAnswer.equals("q")) {
        exit = false;
        redo = false;
        System.out.println("Goodbye!");
        continue;
      } else {
        System.out.println("Sorry, I didn't catch that.");
        continue;
      }
    } while (exit);

  } while (redo);
} //end main
} //end class