I have no problem using a method int to run this program; however I wanted to be able to learn how to do the void method.
I know the return statement isn't necessary in void, and for void with two int parameters. In my book it said parameters should be written as (int a, int b).
However, in my code for lines 16 and 17
sum = computeSum(num1, num2);
product = computeProduct(num1, num2);)
I get the error incompatible types, void cannot be converted to int.
How do I rectify this for future reference? Thanks so much!
import java.util.Scanner;
public class Lab6
{
public static void main (String [] args)
{
//create a scanner object for receiving user input
Scanner keyboard = new Scanner (System.in);
int num1, num2, sum, product;
System.out.println ("Please enter an integer: ");
num1 = keyboard.nextInt();
System.out.println ("Please enter another integer: ");
num2 = keyboard.nextInt();
sum = computeSum(num1, num2);
product = computeProduct(num1, num2);
}
public static void computeSum(int num1, int num2)
{
int sum = 0;
sum = num1 + num2;
System.out.println ("The sum of your integers is " + sum);
}
public static void computeProduct(int num1, int num2)
{
int product = 0;
product = num1 * num2;
System.out.println("\nThe product of your integers is: " + product);
}
}