简体   繁体   English

Java:在方法main中调用方法

[英]Java: calling a method in method main

FYI: I am a beginner. 仅供参考:我是初学者。 Also, I understand that calling methods is a novice concept and there are a few threads like this already. 另外,我理解调用方法是一个新手概念,并且已经有一些这样的线程。 My situation is a little different because I am very restricted by pseudo-code that my program must mirror identically. 我的情况有点不同,因为我受到伪代码的限制,我的程序必须完全相同。 I am having trouble calling methods from other methods, including calling a method from inside main. 我无法从其他方法调用方法,包括从main内部调用方法。 Here is the pseudo-code followed by the code that I wrote: 这是伪代码,后跟我写的代码:

PSEUDO-CODE: 伪代码:

// 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: JAVA代码:

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

My main issue is calling the IsPrime method within the if statement. 我的主要问题是在if语句中调用IsPrime方法。 I get an error saying the IsPrime cannot be converted from int to boolean which I knew, but the pseudo-code restricts me from doing much else. 我得到一个错误,说IsPrime不能从int转换为我知道的boolean,但是伪代码限制我做其他事情。 I also would like advice on how I should call the PrintNPrimes method within method main. 我也想了解如何在方法main中调用PrintNPrimes方法。 Thanks. 谢谢。

Because your PrintNPrimes is static method, you can just call the method by passing the numberofPrimes . 因为您的PrintNPrimes是静态方法,所以您可以通过传递numberofPrimes来调用该方法。

Example: 例:

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

Note: Java naming convention suggests that use first letter as small case letter while defining methods. 注意:Java命名约定建议在定义方法时使用第一个字母作为小写字母。

You can follow same approach to invoke other methods. 您可以按照相同的方法调用其他方法。

if IsPrime(myNum) 如果IsPrime(myNum)

needs to be 需要是

if (IsPrime(myNum)) if(IsPrime(myNum))

Also be sure to restore your curly braces. 还要确保恢复花括号。 I don't see any reason why this will cause an error. 我没有看到任何原因导致错误。 Please post the exact error message if you still have problems. 如果您仍有问题,请发布确切的错误消息。

Update code below with resolution for both (including if statement) of your compilation errors: 更新下面的代码, 包含编译错误的两个(包括if语句)的解决方案:

printNPrimes(numberOfPrimes);

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

Full updated code: 完整更新的代码:

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't check the logic. 我没有检查逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM