【java】编写程序,要求判断从键盘输入的字符串是否为回文
publicclassPalindrome{publicstaticvoidmain(Stringargs[]){Stringmessage;System.out.pri...
public class Palindrome {
public static void main(String args[]) {
String message;
System.out.println("请输入要判断的字符串:");
Scanner sca = new Scanner(System.in);
message = sca.nextLine();
if(isPalindrome(message)){
System.out.println(message+"是回文");
}else
System.out.println(message+"不是回文");
}
public static boolean isPalindrome(String mess){
boolean truth=true;
for (int x = 0; x <= mess.length()/2; x++) {
if (x != mess.length() - x - 1) {
truth=false;
}
}return truth;
}
}
运行出来以后不论输入什么都说不是回文,用eclipse保存的时候还出现框 展开
public static void main(String args[]) {
String message;
System.out.println("请输入要判断的字符串:");
Scanner sca = new Scanner(System.in);
message = sca.nextLine();
if(isPalindrome(message)){
System.out.println(message+"是回文");
}else
System.out.println(message+"不是回文");
}
public static boolean isPalindrome(String mess){
boolean truth=true;
for (int x = 0; x <= mess.length()/2; x++) {
if (x != mess.length() - x - 1) {
truth=false;
}
}return truth;
}
}
运行出来以后不论输入什么都说不是回文,用eclipse保存的时候还出现框 展开
展开全部
对于回文这个问题,我们一般的解决方法是用递归
package com.axjy.yzn;
public class Recursive {
public static void main(String[] args) {
System.out.println("Is abcba a Palindrome?"+isPalindrome("abcdcba"));
}
public static boolean isPalindrome(String s){
if(s.length()<=1){
return true;
}else if(s.charAt(0) != s.charAt(s.length()-1)){
return false;
}
return isPalindrome(s.substring(1,s.length()-1));
}
}
package com.axjy.yzn;
public class Recursive {
public static void main(String[] args) {
System.out.println("Is abcba a Palindrome?"+isPalindrome("abcdcba"));
}
public static boolean isPalindrome(String s){
if(s.length()<=1){
return true;
}else if(s.charAt(0) != s.charAt(s.length()-1)){
return false;
}
return isPalindrome(s.substring(1,s.length()-1));
}
}
展开全部
用Java编写如下:
import java.util.Scanner;
public class PalindromeString {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入你的字符串:");
String str = in.nextLine().toString();
int temp = 0;
for(int i = 0; i < str.length()/2; i++) {
if(str.charAt(i) != str.charAt(str.length()-1-i)) {
temp = 1;
break; }
}
if(temp == 1)
System.out.print("该字符串不是回文字符串");
else
System.out.print("该字符串是回文字符串");
in.close();
}
}
import java.util.Scanner;
public class PalindromeString {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入你的字符串:");
String str = in.nextLine().toString();
int temp = 0;
for(int i = 0; i < str.length()/2; i++) {
if(str.charAt(i) != str.charAt(str.length()-1-i)) {
temp = 1;
break; }
}
if(temp == 1)
System.out.print("该字符串不是回文字符串");
else
System.out.print("该字符串是回文字符串");
in.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询