java - 为 Java 创建一个在输入/输出后不会关闭的循环回文程序

标签 java

我目前正在大学学习java类(class),我的项目是创建一个回文程序,输入一个字符串并输出该字符串是否是回文,但程序在这样做后无法关闭,就像我的教授一样让我通过键盘输入多个字符串。这是开始代码:

    // CSCI 200 Program 2

    import java.util.*;

    public class Program2
    {
      //
      // method: isPalindrome
      // pre-conditions: a string is passed in
      // post-conditions: return true if the string is a palindrome otherwise return false
      //

      public static Boolean isPalindrome(String s)
      {
        //Palindrome Code -- this requires a return statement, but i'm not sure 
        //what.
      }

    public static void main(String[] args)
    {
      Scanner s = new Scanner(System.in);
      System.out.println("String: ");
      String word = s.nextLine();
      //
      // keep reading words until the word QUIT is read in
      //
      while (!word.equals("QUIT"))
      {
        //
        // call the isPalindrome method passing it the word
        // based on what this method returns (true or false) output a message
        //
        if (isPalindrome(word))
            System.out.println("the string [" + word + "] IS a palindrome.");
        else
            System.out.println("the string [" + word + "] IS NOT a palindrome.");
        word = s.nextLine();
      }
    }
  }

在方法isPalindrome下,它说它需要一个return语句,但我不确定它是什么,并且我已经挠头两天多尝试不同的代码和return声明。任何帮助将不胜感激。

我自己也制作了一个有效的回文程序,但我似乎无法让它重复,它只需要一个字符串(例如雷达),说它是一个回文,然后退出。

谢谢。

最佳答案

isPalindrome 方法的返回类型是 Boolean,这就是为什么它需要 Boolean 类型的返回语句或者 true > 或。因此,如果给定字符串字符串的反转等于它的值,则返回true,否则返回false。以下是工作片段:

public class Program2{

    public static Boolean isPalindrome(String string) {
        if (null == string)
            return false;
        String reverse = new StringBuffer(string).reverse().toString();
        if (string.equals(reverse))
            return true;
        return false;
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("String: ");
        String word = s.nextLine();
        while (!word.equals("QUIT")) {
            if (isPalindrome(word)) {
                System.out.println("The string [" + word + "] IS a palindrome.");
            } else {
                System.out.println("The string [" + word + "] IS NOT a palindrome.");
            }
            System.out.println("String: ");
            word = s.nextLine();
        }
        s.close();
    }
}

输出:

String: 
test
The string [test] IS NOT a palindrome.
String: 
madam
The string [madam] IS a palindrome.
String: 
QUIT

关于java - 为 Java 创建一个在输入/输出后不会关闭的循环回文程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60201361/

相关文章:

java - 闹钟应用程序防止系统时间更改

java - 无法从 VisualVM 通过 JMX+SSL 进行连接

Java Swing JTextPane text/html 不支持禁用属性

java - 休息模板。从八位字节流中解码 JaxB 对象

java - 未找到 Gradle 项目依赖项

java - JTable 添加新行

java - 使用 Java 自动化 Web 操作

java - 不使用接口(interface)将数据从 Activity 传递到非 fragment/Activity 类

java - 一般来说,为 Tomcat REST 服务配置合理的最大线程数限制,还是将其设置为有效的无限值,哪个更好?

java - Eclipse 版本 3.6.1 : where might jar names be referenced other than . 类路径文件?