在Java上执行此回文有困难吗

在Java上执行此回文有困难吗,java,Java,我学习java很困难,我希望在这方面能得到一些帮助。我试图让用户输入一个单词,让系统检查它是否是回文。我从其他人那里获取了一些代码以获得帮助,但我被卡住了 import java.util.Scanner; class PalidromeTester { public static boolean isPalindrome (String[] word) { for (int i=0; i< word.length; i++) { if

我学习java很困难,我希望在这方面能得到一些帮助。我试图让用户输入一个单词,让系统检查它是否是回文。我从其他人那里获取了一些代码以获得帮助,但我被卡住了

import java.util.Scanner;

class PalidromeTester
{

    public static boolean isPalindrome (String[] word) {
       for (int i=0; i< word.length; i++) { 
            if (!word[i].equals( word[word.length-1-i])){
                return false; 
            }
        }
        return true; 

    // read word
    public static String readWord(Scanner input){
      String word = input.nextLine();
      word = word.toLowerCase();
      return word;
    }

    // display results to the screen
    public static void displayResults(boolean result){
      // display the results
      String msg = "\nThat string ";
      msg += (result == true) ? " IS " : " is NOT ";
      msg += " a palindrome.\n";
      System.out.println(msg);
    }

    // main function
    public static void main(String[] args){
      // input scanner
      System.out.print('\u000C');
      System.out.println("Enter the word: ");
      Scanner input = new Scanner(System.in);
      PalidromeTester.readWord(input);
    }
}

假设你需要,我想这就是你需要的。我已经对代码本身所做的更改进行了注释,以向您指出为什么首先要做这些更改。还要记住,解决问题有多种方法,这只是其中一种可能性。请随意创新并添加您自己的解决方法

import java.util.Scanner;

public class Palindrome { //<-- added public to the class otherwise main method won't be called

public static boolean isPalindrome(String word) { //<--changed the String[] to String
    for (int i = 0; i < word.length(); i++) {
        if (!(word.charAt(i) == word.charAt(word.length() - 1 - i))) { //Since [] will not work on Strings, using charAt() to do the same thing
            return false;
        }
    }
    return true;
}

// read word
public static String readWord(Scanner input) {
    String word = input.nextLine();
    word = word.toLowerCase();
    return word;
}

// display results to the screen
public static void displayResults(boolean result) {
    // display the results
    String msg = "\nThat string ";
    msg += (result == true) ? " IS " : " is NOT ";
    msg += " a palindrome.\n";
    System.out.println(msg);
}

// main function
public static void main(String[] args) {
    // input scanner
    System.out.print('\u000C');
    System.out.println("Enter the word: ");
    String s = readWord(new Scanner(System.in)); //Added a call to the readWord method and also passed a Scanner reference to the method and 
                                                 //storing the read word in String s
    boolean result = isPalindrome(s); //Added a call to the palindrome method and also passing the read string s to the method to find whether
                                      //it is palindrome or not and storing the method return value in boolean result variable
    displayResults(result); //Finally calling the displayResults with the result we got from isPalindrome() to display the appropriate message
}
}

怎么,怎么,你被困在哪里了?您当前的问题描述太模糊。我无法在运行Main时输入word您从未调用readWord方法。如果你不给它打电话,它就不会被执行。提示:publicstaticvoidmain是在运行程序时执行的入口方法。看看这个方法没有什么much@Aomin字符串生成器版本的空间需求和运行时间增加了一倍。某些作业对这些内容要求非常严格。@Aominè他不调用主方法中的任何内容,也不调用readWord方法。