1. java ')' 预期错误 + 2. 'else' 没有 'if' 错误

1. java ')' expected error + 2. 'else' without 'if' error

第8行遇到错误')' expected;错误 'else' without 'if' 出现在第 9 行。 这只是方法的一部分。开头声明了所有需要的变量(name1、name2、count),并声明了读取文件时可能出现的异常。此时,程序应该已经在读取文件,以便比较文件中写入的名称。

 while ( ! TextIO.eof() )
            do {
                name1.compareTo(name2);
                if (name1.equals(name2));
                count++;
            } while ( ! TextIO.eof() );





    if (count >= 0){
        System.out.println("You encountered" + count "identical names.");
    else
        System.out.println("There was no name encountered more than once.");
    }

删除 if 语句末尾的 ;; 结束 if 语句。

if (name1.equals(name2))
count++;

并分别为 ifelse 添加大括号。

if (count >= 0)
{
    System.out.println("You encountered" + count + "identical names.");
}
else
{
    System.out.println("There was no name encountered more than once.");
}   
if (count >= 0){
    System.out.println("You encountered" + count "identical names.");
else
    System.out.println("There was no name encountered more than once.");
}

应该改为

if (count >= 0){
    System.out.println("You encountered" + count "identical names.");
} else {
    System.out.println("There was no name encountered more than once.");
}

您必须从 if 语句的末尾删除 ;,就是这样。