简体   繁体   中英

Passing integer from one method to another

Trying to write a program with two methods. First one takes an integer and prints its divisors as well as the sum of its divisors, the second is a boolean function that returns if the given integer is equal to the sum of its divisors (a perfect number).

I have the first method done fine but I want to take the integer sum from it and use it in the second method, is this even possible? Iv spent about quite a while trying to research it with no success.

As usual any and all advice appreciated. Code so far is below.

import java.util.Scanner;
import java.io.*;

class arrayTest {

public static int sumFacs(int n) {

    int sumDiv[] = new int[50];
    int c = 0;
    int sum=0;

    if(n<0){
        System.out.println("Sorry I dont do negatives!");
    }

    else {

    for(int i=1; i<=n; i++){
        int j = n%i;

        if(j==0){
            System.out.println( i + " is a divisor of " + n + "\n");
            sumDiv[c] = i;
            c++;
        }

    }

    for (int i=0; i<sumDiv.length; i++){
    sum += sumDiv[i];}

    System.out.println("The sum of the divisors of " + n + " is: " + sum);

}
    return sum;
}

public static boolean isPerfect(int n, int sum) {

boolean b = (n == sum);
if(b){
    System.out.println( n + " is a perfect number.");
}

else {
    System.out.println( n + " is not a perfect number.");
}
}




public static void main(String[] args){

Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter a positive integer.");
    int n = scanner.nextInt();

 sumFacs(n);
 int sumDivisors = sumFacs(n);
 System.out.println(isDivisors(sumFacs(n), n));

 isPerfect(n, sum);
}
}

Your sumFacs returns an integer, so you can definitely use whatever it yields in your isPerfect method. As you are doing it now, you are passing the same number to the methods. To do what you want, you will need to do something like so:

int sumFacsResult = sumFacs(n);     //Take whatever value sumFacs yields, store it in sumFacsResult
System.out.println(isPerfect(sumFacsResult, n);    //Take the result of sumFacs and pass it to the isPerfect method

or shorthand:

System.out.println(isPerfect(sumFacs(n), n));    //It is usually recommended to use the other approach, for readability purposed.

And your isPerfect method should be something like so:

public static boolean isPerfect(int sum, int number)
{
    return sum == number;
}

your sumFacs(int n) has a return type of int and you are returning sum but where you call that method you are not setting it to anything so what you want to do is:

int foo = sumFacs(n);  
isPerfect(n,foo);

and make your isperfect class

public static void isPerfect(int n, int foo)
{ 
  if((n==foo) 
  {     
    System.out.println( n + " is a perfect number."); 
  }else{    
    System.out.println( n + " is not a perfect number."); 
  }
} 

However this would all be easier in one class not two

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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