Java:在方法main中调用方法

时间:2012-10-08 03:30:28

标签: java methods static main

仅供参考:我是初学者。另外,我理解调用方法是一个新手概念,并且已经有一些这样的线程。我的情况有点不同,因为我受到伪代码的限制,我的程序必须完全相同。我无法从其他方法调用方法,包括从main内部调用方法。这是伪代码,后跟我写的代码:

伪码:

// 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

JAVA CODE:

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

我的主要问题是在if语句中调用IsPrime方法。我得到一个错误,说IsPrime不能从int转换为我知道的boolean,但是伪代码限制我做其他事情。我也想了解如何在方法main中调用PrintNPrimes方法。谢谢。

3 个答案:

答案 0 :(得分: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);
    }
  ..........

注意:Java命名约定建议在定义方法时使用第一个字母作为小写字母。

您可以按照相同的方法调用其他方法。

答案 1 :(得分:1)

如果IsPrime(myNum)

需要

if(IsPrime(myNum))

还要确保恢复花括号。我没有看到任何原因导致错误。如果您仍有问题,请发布确切的错误消息。

答案 2 :(得分:1)

更新以下代码,并解析编译错误的(包括if语句)

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

我没有检查逻辑。