简体   繁体   English

奇怪的语法错误,插入}以完成阻止

[英]strange Syntax Error, Insert } to Complete Block

I'm writing a program on finding the prime number and I ran into a syntax error 'Syntax Error, Insert } to Complete Block' when I added in the 'else if'. 我正在写一个查找素数的程序,当我在“ else if”中添加时遇到了语法错误“ Syntax Error,Insert} to Complete Block”。

I've marked in asterisks where I'm getting the error. 我在出现错误的地方标了星号。

import java.math.*;

public class FindThePrime {

    public static void main(String[] args) {

        int x = 2; 
        int nextPrime =  1;
        for (int i = 1; i <= 10; ++i){



        int count = 0;

        while (nextPrime > x);{

            if (nextPrime % x == 0);{


                ++nextPrime; 
                x = 2;
                ++count;

            }
            if (nextPrime < x); {
                ++x;
            }
        ***}***



    }
        else if{

        }
    }

}

I read other reports of the same problem and the aren't any help. 我阅读了有关同一问题的其他报告,但没有任何帮助。 I counted the number of { & } and they come up to an even number. 我计算了{&}的数量,得出了偶数。

Any suggestions? 有什么建议么?

ps: This project is far from complete so forgive the roughness of it. ps:这个项目远未完成,因此请原谅它的粗糙性。 Also, don't tell me how to get the results I want, I don't want to spoil the challenge! 另外,不要告诉我如何获得想要的结果,我不想破坏挑战!

Your else if needs to be directly after the if. 您的else if必须直接在if之后。 Right now you are saying while { do stuff } elseif { do other stuff } 现在,您在说{做其他事情} elseif {做其他事情}

So try 所以尝试

        if (nextPrime < x) {
            ++x;
        }
        else if {

        }

also if you are using eclipse you can use CTRL-SHIFT-F to format the code (if it has no errors). 如果使用的是eclipse,也可以使用CTRL-SHIFT-F格式化代码(如果没有错误)。 This can really help sort out nesting issues. 这确实可以帮助解决嵌套问题。

Most importantly you have a ; 最重要的是你有一个; after the while and the if. 一会儿之后,如果。 A ; 一种 ; indicates that the command will not include {}s, so you want to remove the ; 表示该命令将不包含{},因此您要删除;;

For example while (1==1); 例如while(1 == 1); will loop forever. 将永远循环。

import java.math.*; 导入java.math。*;

public class FindThePrime { 公共类FindThePrime {

public static void main(String[] args) {

    int x = 2; 
    int nextPrime =  1;
    for (int i = 1; i <= 10; ++i)
    {
       int count = 0;
       while (nextPrime > x)
       {
         if (nextPrime % x == 0)
         {
            ++nextPrime; 
            x = 2;
            ++count;
          }
          if (nextPrime < x) {
            ++x;
          }   
          else {
              System.out.println("foo");
          }

     } //while loop

    }//for loop
}//main

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM