在回文中forloop [英] forloop in palindrome

查看:237
本文介绍了在回文中forloop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在练习一个字符串回文。我的代码在我的for循环吗?
$ b $ pre $ public static void main(String args [])
{
String word = JOptionPane.showInputDialog(Input a String:);
String finalword = word.replaceAll(,)。toLowerCase();

for(int x = word.length(); x> = word.length() - 1; x--)
{
finalword.charAt(x) ;


if(word.equals(finalword))
{
JOptionPane.showMessageDialog(null,Palindrome);
}
else
{
JOptionPane.showMessageDialog(null,Not a Palindrome);


$ / code $ / pre
$ b $ p

感谢您的问候。

解决方案


  1. 您需要处理的实际字符串是finalword,因为它的长度对您很重要。为简单起见,需要tempStart和tempLast这两个临时数组,它们的长度必须与(finalword.length / 2)的长度相同,不要担心字符串有偶数或者奇数个字符。

  2. 使用两个变量的循环,如下所示:

      for(int x = 0,y = finalword.length(); x  //检查tempStart和tempLast是否相同的字符。 
    //我希望你知道回文的规则。如果这两个数组(tempStart和tempLast)匹配,那么它的回文否则不。

我希望这能帮到你。如果没有让我知道你需要更多的帮助,而这样做。


I am practising a string palindrome. Is my code in my for loop right?

public static void main (String args[]) 
{
    String word = JOptionPane.showInputDialog("Input a String:");
    String finalword = word.replaceAll(" ","").toLowerCase();

    for (int x = word.length(); x >= word.length()-1; x--) 
    {
        finalword.charAt(x);
    }

    if(word.equals(finalword)) 
    {
        JOptionPane.showMessageDialog(null, "Palindrome");
    }
    else 
    {
        JOptionPane.showMessageDialog(null, "Not a Palindrome");
    }
}    

Thank you and best regards.

解决方案

  1. The actual string you need to work on is finalword as its length important to you.

  2. For simplicity take two temp arrays like tempStart and tempLast, both has to be same length as (finalword.length /2), Dont worry about string has even or odd number of chars.

  3. use for loop with two variables as follows.

     for(int x = 0, y=finalword.length(); x<finalword.length() / 2; x++,y--){
       // check tempStart and tempLast has same chars.
       // I hope you know rule for palindrome.
     }
    

  4. if these both arrays (tempStart and tempLast) matches then its palindrome otherwise not.

I hope this will help you. If not let me know you need any more help while doing this.

这篇关于在回文中forloop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆