问题详情

判断一个字符串是否是回文。回文是指:字符串从左向右读与从右向左度是一样的。


时间:2021-07-17 17:55 关键词:

答案解析

<font face="宋体" size="3"> </font><p><span>判断一个字符串是否是回文。回文是指:字符串从左向右读与从右向左度是一样的。</span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri">import java.util.Arrays;</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri">public class ExamDemo {</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>public static void main(String[] args){</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>String s = args[0];</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>char[] str = s.toCharArray();</font></span></p><font face="宋体" size="3"> </font><p><font face="Calibri"><span lang="EN-US"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span lang="NO-BOK">int i;</span></font></p><font face="宋体" size="3"> </font><p><span lang="NO-BOK"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>for (i = 0; i &lt; str.length / 2; i++){</font></span></p><font face="宋体" size="3"> </font><p><font face="Calibri"><span lang="NO-BOK"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span lang="EN-US">if (str[i] != str[str.length - i - 1])</span></font></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>break;</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if (i == str.length / 2)</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>System.out.println("</font></span><span>字符串</span><span lang="EN-US"><font face="Calibri">" + args[0] + "</font></span><span>是回文</span><span lang="EN-US"><font face="Calibri">");</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>else</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>System.out.println("</font></span><span>字符串</span><span lang="EN-US"><font face="Calibri">" + args[0] + "</font></span><span>不是回文</span><span lang="EN-US"><font face="Calibri">");</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</font></span></p><font face="宋体" size="3"> </font><p><span lang="EN-US"><font face="Calibri">}</font></span></p><font face="宋体" size="3"> </font><p><br></p>