在java中调用方法有问题

时间:2016-11-07 00:17:13

标签: java methods primes

好的,我正在尝试编写一个可以执行三项操作的程序。要求用户输入三个数字。接下来我想要的是调用一种方法来对这些数字求和并显示它们。然后我想调用另一种方法来确定总和是否为素数。现在,我知道如何做几乎所有这些,除了我不确定我如何理解如何调用新方法,以及目的是什么。我可以编写一个执行所有三个步骤的程序,但每次都不需要调用新方法。我该怎么办?这样做的目的是什么,而不是在主方法下呢?

如果您需要我发布我可以执行的三个步骤的代码,也谢谢您的帮助!

编辑:到目前为止,这是我的代码:

    public static void main(String[] args) {
    // Ask for numbers
    Scanner scan = new Scanner(System.in);

    int num1 = scan.nextInt();
    int num2 = scan.nextInt();
    int num3 = scan.nextInt();

    int sum = sum(num1, num2, num3);
    System.out.println(sum);

    boolean isPrime = isPrime(sum); 


}

    public static int sum(int number1, int number2, int number3) {

        int sum = 0;

        sum = (number1 + number2 + number3);

        return sum; 
    }

    public static boolean isPrime(int sum) {

        boolean prime = true;
        for(int p = 2; p < sum; p++) {
            if(sum % p == 0)
                prime = false;

        }
    }
}

我感到困惑的两件事是如何在最后一种方法中找出素数和返回结果,而且我也不完全理解这些额外方法的要点。我相信在我可以返回结果之后我就可以打印出结果了。但是我不确定如何到达那里。

Edit2:对于后来看的人,我想出来,我相信,这是我的更新代码

public static void main(String[] args) {
    // Ask for numbers
    Scanner scan = new Scanner(System.in);

    int num1 = scan.nextInt();
    int num2 = scan.nextInt();
    int num3 = scan.nextInt();

    int sum = sum(num1, num2, num3);
    System.out.println("The sum of your numbers is " + sum + ".");

    boolean isPrime = isPrime(sum); 
    if (isPrime)
        System.out.println("\nThe sum is a prime number.");
    else 
        System.out.println("\nThe sum is not a prime number.");



}

    public static int sum(int number1, int number2, int number3) {

        int sum = 0;

        sum = (number1 + number2 + number3);

        return sum; 
    }

    public static boolean isPrime(int sum) {

        boolean isprime = true;
        for(int p = 2; p < sum; p++) {
            if(sum % p == 0)
                isprime = false;    
        }
        return isprime;
    }
}

1 个答案:

答案 0 :(得分:0)

对方法的调用是在以下两行:

int sum = sum (num1, num2, num3);

boolean isPrime = isPrime(sum);

它的作用是调用方法然后调用方法返回的值在方法调用之前存储在变量中(sum和isPrime);

public class Main {

    public static void main(String[] args) {

        System.out.println("In the main method");
        /*
         * Get the users input (3 numbers)
         */

        int sum = sum(num1, num2, num3);//This will call the sum method which will calculate the sum and return it(store it in this sum variable)

        boolean isPrime = isPrime(sum);//Calls on the 'isPrime(...)' method Stores true if sum is prime, false otherwise. 

    }
    /**
     * Calculates the sum of a 3 numbers
     * 
     * @param number1
     * @param number2
     * @param number3
     * @return sum The sum of 3 numbers
     */
    public static int sum(int number1, int number2, int number3) 
    {
        int sum = 0;
        /*
         * Calculate the sum
         */
        return sum;
    }

    /**
     * Determines if a number is a prime
     * 
     * @param sum   variable which is being checked for prime
     * @return true If the sum is a prime
     */
    public static boolean isPrime(int sum) {
        /*
         * Determine if the sum is a prime
         */
        return true;//Or false depending if it is a prime
    }
}