求帮忙写一个JAVA程序
Writeaprogramtoinputastringsentenceandprintitinreverseorder.Alsodisplayamessageastowh...
Write a program to input a string sentence andprint it in reverse order. Also display a message as to whether the sentence isa palindrome or not.
展开
展开全部
package zcb;
import java.util.Scanner;
public class Main {
public Main() {
Scanner sc = null;
try {
sc = new Scanner(System.in);
System.out.println("Enter a sentence");
String inputStr = sc.nextLine();
char[] c1 = inputStr.toCharArray();
int length = c1.length;
char[] c2 = new char[length];
for (int i = 0; i < c2.length; i++) {
c2[i] = c1[length - 1 - i];
}
String reversedStr = new String(c2);
System.out.println("The input line when reversed is:\n" + reversedStr);
if (inputStr.equals(reversedStr)) {
System.out.println("Your input was a palindrome");
} else {
System.out.println("Your input was not a palindrome");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sc != null) {
sc.close();
}
}
}
public static void main(String[] args) {
new Main();
}
}
追问
你好 这段代码我运出来显示第四行 "public Main() {" 有错误
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询