Java 布尔值 Return 错误
Java Boolean Return Errors
我是 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
我浏览了该网站上的许多问题,寻找可以解释这些问题的内容,但一无所获。谁能告诉我为什么会出现这些错误?
您的代码中有几个错误。
首先你在另一个方法中定义一个新方法。这在 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))
所以您的 class 必须如下所示:
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;
}
}
我是 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
我浏览了该网站上的许多问题,寻找可以解释这些问题的内容,但一无所获。谁能告诉我为什么会出现这些错误?
您的代码中有几个错误。
首先你在另一个方法中定义一个新方法。这在 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))
所以您的 class 必须如下所示:
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;
}
}