第三次java测验1

设计思想:

输入一个字符串,然后将字符串倒置,比较字符串第i位上的字符与倒数第i位上的字符是否相同,如果都相同则字符串是回文;否则字符串不是回文。

源代码:

package java3;
import java.util.Scanner;

public class First {
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("请输入一个字符串:");
Scanner input=new Scanner(System.in);
String str = input.nextLine();
StringBuffer sb=new StringBuffer(str);
sb.reverse();//将字符串倒置
int count=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==sb.charAt(i))//判断字符串的第i位上字符与倒数第i位上字符是否相同
{
count++;
}
}
if(count==str.length())
{
System.out.println("字符串 "+str+" 是回文");
}
else
{
System.out.println("字符串 "+str+" 不是回文");
}
}
}

运行测试结果:

原文地址:https://www.cnblogs.com/zaixiachengxuyuan/p/11583972.html