循环后清除java字符串

时间:2016-03-03 02:52:32

标签: java string clear palindrome

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

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

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

感谢您的帮助!另请注意,可能有更好或更快的方法来完成我在这里所做的事情,但我对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

2 个答案:

答案 0 :(得分:1)

在while循环结束时添加reverse = "";

答案 1 :(得分:0)

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

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

第一个循环内部。虽然你可以简单地在循环结束时重置两个变量......

input = "";
reverse = "";        

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

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