【问题标题】:Weird errors on Prime Number &twins code?素数和双胞胎代码的奇怪错误?
【发布时间】:2014-03-14 20:29:29
【问题描述】:

所以我正在做这个素数家庭作业,并举了一个很好的例子,我认为我已经完成了大部分内容。我遇到的一件事是在“public static void sieve (int n)”行出现错误,该错误也发生在“private static int twinPrime()”

代码如下:

import java.util.*;

public class PrimeNumbers
{
public static void main (String [] args)
{
    Scanner in = new Scanner(System.in);
    
    System.out.print("Enter a value for n: ");
    int n = in.nextInt();
    in.nextLine(); // clear input buffer
    System.out.println();
    
    // Generate a list of prime numbers between 2 and n
    // to test Part 1
    ArrayList<Integer> primes = sieve(n);
    System.out.println("Prime numbers between 2 and " + n + ":\n");
    System.out.println(primes);
    System.out.println();
    
    // Test Part 2
    System.out.println("The twin primes less than " + n + " are:\n");
    twins(n);
    System.out.println();
}

// COMPLETE THIS METHOD FOR PART 1
public static ArrayList<Integer> sieve (int maxValue)
{
    public static void sieve (int n)
    {
        // 1 = assumed prime, 0 = known not prime
        // Create a list of numbers from 0-n
        ArrayList<Integer> sieve =
                         new ArrayList<Integer>(n+1);
        // Fill initial sieve with non-zeros (assumed prime)
        int index;
        
        for (index = 0; index <= n; index++)
        {
            sieve.add(index);
        }
        
        System.out.println("Starting sieve: " + sieve);
        
        // For each position/value >= 2, if it's 1,
        // cross off its later multiples (set them to 0)
        for (index = 2; index < sieve.size(); index++)
        {
            if (sieve.get(index) > 0)
            {
                System.out.println(index + " is prime");
                
                // Mark off multiples of index
                int mult = 2 * index; // first multiple
                for (; mult < (n+1); mult += index)
                {
                    // Increment by (index) each time
                    // set() takes position, new value
                    sieve.set(mult, 0); // mark off value
                }
                System.out.println(sieve);
            }
        }
        
        // Remove all non-prime values
        sieve.remove(0);
        sieve.remove(0);
        
        for (index = 0; index < sieve.size(); index++)
        {
            if (sieve.get(index) == 0)
            {
                sieve.remove(index);
                index--;
            }
        }
        
        System.out.println(sieve);
    
}}

// COMPLETE THIS METHOD FOR PART 2
public static void twins (int max)
{
    // ADD YOUR CODE HERE
}
}

以下是错误:

void 是变量 sieve 的无效类型

标记 "(", ; 应有语法错误

标记 ")" 的语法错误,;预计

标记“int”的语法错误,@预期

语法错误,插入“EnumBody”来完成BlockStatements

语法错误,插入“枚举标识符”以完成 EnumHeaderName

标记“int”的语法错误,@预期

我以前从未见过这样的事情吗?我不知道从哪里开始寻找解释?如果有人可以向我解释错误,我可能可以自己修复代码!

【问题讨论】:

    标签: java primes


    【解决方案1】:

    您正试图在另一个方法中声明一个方法:

    public static ArrayList<Integer> sieve (int maxValue)
    {
        public static void sieve (int n)
        {
    

    在 Java 中,这是不允许的,因此会出现错误。

    你需要弄清楚它是你想要的两个声明中的哪一个,然后去掉另一个(不要忘记删除右花括号)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多