java:24:error:';其他';没有';如果';

java:24:error:';其他';没有';如果';,java,Java,在这个程序中,我想知道它的类型是前缀还是后缀,或者两者都有 例如,我需要5个字。。然后我将从用户word部分获得 如果它是前缀或后缀,或者两者都是,我会选择它 我的代码 import java.util.*; public class part { static Scanner input = new Scanner(System.in); //main method public static void main(String[] args) { System

在这个程序中,我想知道它的类型是前缀还是后缀,或者两者都有

例如,我需要5个字。。然后我将从用户word部分获得

如果它是前缀或后缀,或者两者都是,我会选择它

我的代码

import java.util.*; 
public class part  { 
 static Scanner input = new Scanner(System.in);
//main method 
    public static void main(String[] args) {
        System.out.println("the word Part");
        String wordPart= input.next();
        checkWord( wordPart);

    }
    public static void checkWord(String wordPart) {
        String [] m = {"Unhappy","Designed","liked","important","irregular" };
        for (int i =0 ; i<5;i++){
          if (m[i].startsWith(wordPart)&& m[i].endsWith(wordPart));{
          System.out.println("both");}
          else if (m[i].startsWith(wordPart)){
          System.out.println("prefix");}
          else
          System.out.println("suffix ");}
         }


     }
 }

删除
if
条件末尾的分号。改变

if (m[i].startsWith(wordPart)&& m[i].endsWith(wordPart));{

分号充当
if
条件的主体,使
else
悬空

      if (m[i].startsWith(wordPart)&& m[i].endsWith(wordPart));{
                                                              ^----

所示的
完全终止if(),这意味着没有
if()
else
,正如错误所说。

您有一个分号,其中表示:

if (m[i].startsWith(wordPart)&& m[i].endsWith(wordPart));{
应该是

if (m[i].startsWith(wordPart)&& m[i].endsWith(wordPart)){

更好地格式化代码,您将看到:

for(int i = 0; i < 5; i++)
{
    if(m[i].startsWith(wordPart) && m[i].endsWith(wordPart)); <<<--- Error!
    {
        System.out.println("both");
    }
    else if(m[i].startsWith(wordPart))
    {
        System.out.println("prefix");
    }
    else
        System.out.println("suffix ");
}
for(int i=0;i<5;i++)
{
if(m[i].startsWith(wordPart)和&m[i].endsWith(wordPart));@user2957980
正如大家所说的,分号是问题的根源。请注意;在java中是一个分隔符,因此分隔了每条指令。当您说“return true”时,指令结束于此,因此后面跟着一个分号

对于if语句,如果满足条件,则应该有一个操作

          if (a) then do action b 
您的代码大致翻译为

          if (a) then do nothing. 

希望这能澄清。

如果
,在你的中的条件之后是否应该有一个;?这是一个反问句。使用IDE。当我使用return时,可能会重复。如果我这样写,可以吗return true;return false;}@user2957980当然,如果返回的函数类型为
bool
,则该操作将起作用。当然,你不会一个接一个地写这两个陈述。您将有条件地返回其中一个。
          if (a) then do action b 
          if (a) then do nothing.