Java布尔返回错误

时间:2015-02-16 19:26:19

标签: java boolean

我是java新手(以及一般的编程),并决定制作一个程序来检查数字是否为素数。

我做了一个工作计划:

import java.io.*;

public class PrimeChecker
{
  public static void main (String[] args) throws IOException
  {
    BufferedReader Reader = new BufferedReader (new InputStreamReader (System.in));

    int factor = 2;
    boolean check = true;
    System.out.println("Please input a number.");
    int number = Integer.parseInt(Reader.readLine ());

    if (number == 1)
    {
      System.out.println(number + " is neither prime nor composite.");
    }
    else
    {
      while (factor <= Math.sqrt(number))
      {
        if (number % factor == 0)
        {
          check = false;
          break;
        }
        factor++;
      }
        if (check)
        {
          System.out.println(number + " is a prime number.");
        }
        else
        {
          System.out.println(number + " is a composite number (not prime).");
        }
    }
  }
}

然后我读了this,决定稍微使用布尔值并将其更改为:

import java.io.*;

public class PrimeCheckerv2
{
  public static void main (String[] args) throws IOException
  {
    BufferedReader Reader = new BufferedReader (new InputStreamReader (System.in));

    int factor = 2;
    System.out.println("Please input a number.");
    int number = Integer.parseInt(Reader.readLine ());

    if (number == 1)
    {
      System.out.println(number + " is neither prime nor composite.");
    }
    else
    {
      while (factor <= Math.sqrt(number))
      {
        public boolean isPrime()
        {
          return (!(number % factor == 0));
        }
        factor++;
      }
        if (isPrime)
        {
          System.out.println(number + " is a prime number.");
        }
        else
        {
          System.out.println(number + " is a composite number (not prime).");
        }
    }
  }
}

我相信我按照我之前阅读的链接页面中的说明完成了所有操作,但我的新代码却收到了以下3个错误:

File: C:\Users\Aion\Documents\DrJava\Other\PrimeCheckerv2.java  [line: 19]
Error: Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration
File: C:\Users\Aion\Documents\DrJava\Other\PrimeCheckerv2.java  [line: 19]
Error: Syntax error, insert ";" to complete BlockStatements
File: C:\Users\Aion\Documents\DrJava\Other\PrimeCheckerv2.java  [line: 19]
Error: Syntax error, insert ";" to complete Statement

我在这个网站上查看了许多问题,寻找可以解释它们但却一无所获的东西。谁能告诉我为什么我会收到这些错误?

1 个答案:

答案 0 :(得分:3)

您的代码中存在多个错误。

首先在另一个内部定义一个新方法。这在java中不起作用。

   public boolean isPrime()
    {
      return (!(number % factor == 0));
    }
    factor++;
  }

所以你必须把它放在代码的其余部分。

第二:该方法必须是静态的,因为您想从静态方法调用该方法。

第三:你必须将参数传递给方法:

public static boolean isPrime(int number, int factor) {
    return number % factor != 0;
}

你必须打电话给它:

if (isPrime(factor, number))

所以你的班级必须如下:

public class PrimeCheckerv2 {
    public static void main(String[] args) throws IOException {
        BufferedReader Reader = new BufferedReader(new InputStreamReader(
                System.in));

        int factor = 2;
        System.out.println("Please input a number.");
        int number = Integer.parseInt(Reader.readLine());

        if (number == 1) {
            System.out.println(number + " is neither prime nor composite.");
        } else {
            while (factor <= Math.sqrt(number)) {
                factor++;
                if (isPrime(factor, number)) {
                    System.out.println(number + " is a prime number.");
                } else {
                    System.out.println(number
                            + " is a composite number (not prime).");
                }

            }
        }

    }

    public static boolean isPrime(int number, int factor) {
        return number % factor != 0;
    }

}