Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用eclipse的Java_Java_String - Fatal编程技术网

使用eclipse的Java

使用eclipse的Java,java,string,Java,String,我刚刚用eclipse用java编写了一个简单的回文程序。我犯了这个错误 for(int i=0;i<len-1;i++) { if(inputString[i]==inputString[(len-i-1)] //error here "The type of the expression must be an array type but it resolved to String" { palindrome = tru

我刚刚用eclipse用java编写了一个简单的回文程序。我犯了这个错误

 for(int i=0;i<len-1;i++)
    {
        if(inputString[i]==inputString[(len-i-1)] //error here "The type of the expression must be an array type but it resolved to String"
        {
            palindrome = true;
            i++;
        }
        else
        {                    
            break;

        }
    }

for(int i=0;i
String
s在Java中不是字符数组,而是字符数组。尝试使用
inputString.charAt(i)
在Java中有一个字符串类;它不能像数组一样对待。正如chrylis所说,要么使用charAt()方法,要么使用tocharray()方法将其转换为字符数组

看起来您在检测是否存在回文方面也有缺陷。如果第一个和最后一个字符匹配,回文将设置为true。但是,如果接下来的两个字符不匹配,它将转到条件的else分支,在那里它将退出for循环。但是,回文仍然设置为true。您甚至可以只使用set在for循环之前将palindrome设置为true,然后仅当字符串不是回文时才会设置为false

对于您想要的比较:

palindrome = true;
for(int i=0; i<len-1; i++)
{
    if(inputString.charAt(i)==inputString.charAt(len-i-1))
    {
        i++;
    }
    else
    {   
        palindrome = false;                 
        break;
    }
}
回文=true;

对于(int i=0;i)并关闭pharanthesis。另一个选项是在比较之前使用s.toCharArray();@AurA True将其转换为char数组,但对实际对象的操作是首选的OO习惯用法,而且OP似乎是Java从C中新引入的。C中的@chrylis可以得到类似“2的阶乘是2”的输出通过编写一段代码Console.WriteLine({0}的阶乘是{1})、FactNum、fact);我们如何在java@Vivekh那是一个完全不同的问题,请把它作为一个新问题发布。