2012-10-08 7 views
1

참고 : 저는 초보자입니다. 또한 메서드를 호출하는 것은 초보자 개념이며 이미 이와 같은 몇 가지 스레드가 있다는 것을 알고 있습니다. 내 프로그램이 똑같이 미러해야하는 의사 코드로 인해 매우 제한적이기 때문에 상황이 약간 다릅니다. 다른 메서드에서 메서드를 호출하는 데 문제가 있습니다. 예를 들어 내부에서 메서드를 호출하는 것입니다. 여기에 내가 쓴 코드 다음 의사 코드는 다음과 같습니다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 메서드를 호출해야하는 방법에 대한 조언을 원합니다. 감사.

+0

정확한 오류 메시지를 복사하여 붙여 넣으십시오. 나는 당신의 바꿔 치기에서 몇 가지 중요한 세부 사항을 잃어 버렸다고 믿습니다. –

+1

'if (isPrime (number)) {'가되어야합니까? –

+1

잘못된 위치에 꽤 많은 수의 중괄호가 있습니다. 'i = 0; '에있는 여는 중괄호는'while '바로 뒤에있는 줄로 이동해야합니다. 또한 System.out.println (myNum); 뒤에 닫는 중괄호가 있어야합니다. i를 1로 바꾸는 것과 함께 IsPrime 내부의 루프에서 i를 증가시켜야한다는 것은 말할 것도 없습니다. 실제 답변을 작성하는 데 너무 지쳐 있지만 여기서는 작동하는 코드 (http://ideone.com/drG9p)가 있습니다. – Lalaland

답변

1

PrintNPrimes은 정적 메서드이기 때문에 numberofPrimes을 전달하여 방금 메서드를 호출 할 수 있습니다.

예 :

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(); 
     PrintNPrimes(numberOfPrimes); 
    } 
    .......... 

참고 : 자바 명명 규칙이 방법을 정의하면서 그 첫 글자를 작은 경우 문자를 사용 제안합니다.

같은 방법으로 다른 방법을 호출 할 수 있습니다.

+0

도움이되었습니다. 감사합니다. – user1727642

1

(IsPrime (myNum에))

은 또한 당신의 중괄호를 복원해야하는 경우 IsPrime (myNum에가)

필요

을 할 수있는 경우. 왜 이것이 오류를 일으키는 지 알 수 없습니다. 여전히 문제가있는 경우 정확한 오류 메시지를 게시하십시오. 당신의 컴파일 오류의 (if 문 포함) 모두 해상도 아래

+0

감사합니다. – user1727642

+0

질문을 닫고 도움이된다고 생각되면 답변을 수락하십시오 (담당자가 충분한 경우). –

1

업데이트 코드 :

printNPrimes(numberOfPrimes);

if (isPrime(myNum)) // Call the IsPrime method (see below) {

전체 업데이트 된 코드 :

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(); 
    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; 
} 

I didn 히 논리를 확인하지 마라.

+0

감사합니다. – user1727642

+0

@ user1727642 .. 그런 다음 답변으로 동의합니다. 대답 외에 화살표를 선택하십시오. –