참고 : 저는 초보자입니다. 또한 메서드를 호출하는 것은 초보자 개념이며 이미 이와 같은 몇 가지 스레드가 있다는 것을 알고 있습니다. 내 프로그램이 똑같이 미러해야하는 의사 코드로 인해 매우 제한적이기 때문에 상황이 약간 다릅니다. 다른 메서드에서 메서드를 호출하는 데 문제가 있습니다. 예를 들어 내부에서 메서드를 호출하는 것입니다. 여기에 내가 쓴 코드 다음 의사 코드는 다음과 같습니다Java : 메소드 메인에있는 메소드 호출
의사 코드 :
// The user enters an integer and the program calculates that many primes
// It uses 3 methods, including the main. All the methods are in the same class
// and should be declared as ‘public static.’
Project Print the First n Primes
Package printTheFirstNPrimesPackage
Class PrintTheFirstNPrimes
Method Main
Declare numberOfPrimes as integer
Print “How many prime numbers do you want?"
Read numberOfPrimes from the keyboard
Call the method: PrintNPrimes(numberOfPrimes)
end Method (Main)
// ***********************************************************
// This method accepts an integer and prints that many prime
// numbers, starting at 2. 2 is the lowest primt number.
// ***********************************************************
Method void PrintNPrimes(int n)
declare i as integer
declare myNum as integer
myNum = 2 // The first prime number
i = 0
loop while i < n // This could be a ‘for’ loop
if IsPrime(myNum) // Call the Isprime method, (see below)
i = i + 1
print myNum
End If
myNum = myNum + 1
end loop
end Method PrintNPrimes
// **********************************************************
// This method accepts an integer and tests to see if it is
// a prime number. If it is prime, the method returns true,
// otherwise it returns false.
// **********************************************************
Method boolean IsPrime(int number)
Declare result as boolean
result = true
declare i as integer
i = 2
loop while i < number
if ((number % i) == 0)
result = false
exit loop
end if
end loop
return result
end Method
end Class
End Package
End Project
자바 코드 :
이package printTheFirstNPrimesPackage;
import java.util.*;
public class PrintTheFirstNPrimes {
public static void main(String [] args) {
int numberOfPrimes;
Scanner primes = new Scanner(System.in);
System.out.println("How many prime numbers do you want?");
numberOfPrimes = primes.nextInt();
// Call the method PrintNPrimes(numberOfPrimes)
}
public static void PrintNPrimes(int n) {
int i;
int myNum;
myNum = 2; // The first prime number
i = 0; {
while (i < n)
// if IsPrime(myNum) // Call the IsPrime method (see below) {
i = i + 1;
System.out.println(myNum);
myNum = myNum + 1;
}
}
public static boolean IsPrime(int number) {
boolean result;
result = true;
int i = 2;
while (i < number) {
if ((number % 1) == 0)
result = false;
}
return result;
}
}
내 주요 문제는 만약 내 IsPrime 메소드를 호출한다 성명서. IsPrime이 int에서 boolean으로 변환 될 수 없다는 오류가 발생하지만 의사 코드는 다른 것들을 수행하지 못하도록 제한합니다. 또한 method main 내에서 PrintNPrimes 메서드를 호출해야하는 방법에 대한 조언을 원합니다. 감사.
정확한 오류 메시지를 복사하여 붙여 넣으십시오. 나는 당신의 바꿔 치기에서 몇 가지 중요한 세부 사항을 잃어 버렸다고 믿습니다. –
'if (isPrime (number)) {'가되어야합니까? –
잘못된 위치에 꽤 많은 수의 중괄호가 있습니다. 'i = 0; '에있는 여는 중괄호는'while '바로 뒤에있는 줄로 이동해야합니다. 또한 System.out.println (myNum); 뒤에 닫는 중괄호가 있어야합니다. i를 1로 바꾸는 것과 함께 IsPrime 내부의 루프에서 i를 증가시켜야한다는 것은 말할 것도 없습니다. 실제 답변을 작성하는 데 너무 지쳐 있지만 여기서는 작동하는 코드 (http://ideone.com/drG9p)가 있습니다. – Lalaland