简体   繁体   English

Java类无法编译

[英]Java class won't compile

Im quite the novice when it comes to programming and im trying to translate this PHP algorithm to Java. 我是编程的新手,并且尝试将PHP算法转换为Java。

function isPrime($n)
{
$i = 2;

if ($n == 2) {
    return true;    
}

while ($i < $n) {
    if ($n % $i == 0) {
        return false;
    }
    $i++;
}

return true;
}
for ($i = 3; $i < 100; $i++) {
if (isPrime($i)) {
    echo $i;
}
}

The only thing i've come up with so far is this. 到目前为止,我唯一想出的就是这个。

public class Primtal {
public static boolean isPrime(int n) 
{
    int i = 2;

    if (n == 2) {
        return true;    
    } 
    while (i < n) {
        if ( n % i == 0) {
            return false;
        }
        i++;
    }

    return true;
}

for(int i = 3; i < 1000; i++){
    if (isPrime(i)) {
        System.out.print(i);
    }
}
}

I realize this look really stupid but i really need to get this to work. 我意识到这种外观确实很愚蠢,但我确实需要使它起作用。 I think the problem lies mostly with the for loop as im currently getting the error illegal start of type there. 我认为问题主要出在for循环上,因为即时通讯目前在此处出现错误的非法起始类型。 Im not really sure how to translate this to Java and i would appreciate any help i can get. 我不确定如何将其转换为Java,我将不胜感激。

I believe the problem with your code is that you've put a for loop in the middle of class declaration, which is incorrect - it needs to be inside some method. 我相信您的代码存在的问题是,您在class声明的中间放置了一个for循环,这是不正确的-它必须位于某个方法内部。 It seems logical in this case to put it in main() , so it's executed when you run your program. 在这种情况下,将其放在main()似乎很合逻辑,因此在运行程序时将执行它。 Maybe something like this: 也许是这样的:

public class Primtal
{
    public static boolean isPrime(int n) 
    {
        int i = 2;

        if(n == 2)
        {
            return true;    
        } 

        while(i < n)
        {
            if(n % i == 0)
            {
                return false;
            }

            i++;
        }

        return true;
    }

    public static void main(String[] args)
    {
        for(int i = 3; i < 1000; i++)
        {
            if(isPrime(i))
            {
                System.out.print(i);
            }
        }
    }
}

(Note the addition of public static void main(String[] args) in the second half of the code.) (请注意,在代码的后半部分添加了public static void main(String[] args) 。)

Oracle has official tutorials on how Java programs need to be structured, and other basics of the language. Oracle提供了有关如何构建Java程序以及该语言其他基础知识的官方教程。 You can find the one related to the main method here . 您可以在此处找到与main方法相关的一种。 Or, to start from the beginning, the full tutorial starts here . 或者,从头开始,完整的教程从此处开始。

you can't write the for loop 你不能写for循环

for(int i = 3; i < 1000; i++){
    if (isPrime(i)) {
        System.out.print(i);
    }
}

directly inside a class . 直接在class

i believe what you wish to do is to have a main method, in which you can have the for loop 我相信你想做的是有一个main方法,其中你可以有for循环

Your for loop needs to be within a method of some sort,so you can put it in the main method: 您的for循环需要位于某种方法内,因此可以将其放入main方法中:

public class Primtal {

    public static void main(String [] args)
    {                
        for(int i = 3; i < 1000; i++)
        {
            if (isPrime(i)) {
            System.out.print(i);
        }
    }

    public static boolean isPrime(int n) 
    {
        int i = 2;

        if (n == 2) {
            return true;    
        } 
        while (i < n) {
            if ( n % i == 0) {
                return false;
            }
            i++;
        }

        return true;
    }

}

The problem is that your for loop isn't in a method. 问题是您的for循环不在方法中。 Enclose it in a main method. 将其包含在main方法中。

public static void main(String[] args) {
   // Your for loop here
}

Also, change print to println , or else all the numbers will appear concatenated together on one line. 另外,将print更改为println ,否则所有数字将串联在一起显示。

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

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