# Problem with Return Function

Printable View

• October 25th, 2010, 10:01 PM
Tracy22
Problem with Return Function
Can someone please tell me how to keep this program the same with the 3 functions but instead of the sum of the 2 numbers I need it to tell me if the 1st number is <> or equal to the second number? I cant figure out how to change it in the 2nd and 3rd function so it returns it and displays it. I've been stuck for hours on something that is probaly simple.
Code Java:

```public static void main(String[] args) {   int num3; int num4; int sum;   //get the first number num3 = getNum(); //get the second number num4 = getNum();   //get the sum of the two numbers sum = getSum(num3, num4);   //Show the result showResult(sum); }   //Read a number from the keyboard and return it. public static int getNum() { //for getting an input from the keyboard Scanner input = new Scanner(System.in);   //An inputted number will be stored in this variable int num;   //prompt the user to enter a number System.out.println("Enter a number: ");   //get a number from the keyboard and it is stored in num num = input.nextInt();   //send back the inputted number return num; }   // public static int getSum(int num3, int num4) { //the sum will be stored in this variable int sum;   //add num1 and num2 and the result is stored in sum sum = num3 + num4;   //send back the sum return sum; }   //Show the result public static void showResult(int sum) { System.out.println("The sum of the two numbers is " + sum); System.out.println("Thank you"); }```
• October 25th, 2010, 10:26 PM
helloworld922
Re: Problem with Return Function
Please surround your code with highlight tags. See my signature for how to do this.

Think about this mathematically:

given two numbers a and b, you can determine which how the two numbers are related to each other by subtracting them. If you know which number is the first one, you are relating the second number to the first one. If the result is greater than 0, then that means the first number is larger than the second number. If it's lower than 0, then that means the second number is bigger than the first number. If the result is exactly 0, then the two numbers are equal.
• October 25th, 2010, 11:31 PM
Tracy22
Re: Problem with Return Function
Hi yes I know the mathematical part, but how can i put it in the return function. All I need is to know how to separate the functions so that one will prompt the user to enter number, read a number from the keyboard and return it. The last function will display the result. I only know how to do it all under one function instead of separate. This example I also only did one number but the user will enter 2 numbers and have them compared to see which is greater or less.
Code Java:

```If(num < 0) { System.out.println(num + " is negative"); } else if(num == 0) { System.out.println(num + " is zero"); } else //Any other numbers should be positive { System.out.println(num + " is positive"); }```
.
• October 25th, 2010, 11:38 PM
helloworld922
Re: Problem with Return Function
Have a compareTo method, which will return the difference of the two numbers. Then use the above logic in your showResult method. Your getNum method doesn't need to be changed.
• October 26th, 2010, 03:32 PM
javapenguin
Re: Problem with Return Function
Quote:

Originally Posted by Tracy22
Can someone please tell me how to keep this program the same with the 3 functions but instead of the sum of the 2 numbers I need it to tell me if the 1st number is <> or equal to the second number? I cant figure out how to change it in the 2nd and 3rd function so it returns it and displays it. I've been stuck for hours on something that is probaly simple.
Code Java:

```public static void main(String[] args) {   int num3; int num4; int sum;   //get the first number num3 = getNum(); //get the second number num4 = getNum();   //get the sum of the two numbers sum = getSum(num3, num4);   //Show the result showResult(sum); }   //Read a number from the keyboard and return it. public static int getNum() { //for getting an input from the keyboard Scanner input = new Scanner(System.in);   //An inputted number will be stored in this variable int num;   //prompt the user to enter a number System.out.println("Enter a number: ");   //get a number from the keyboard and it is stored in num num = input.nextInt();   //send back the inputted number return num; }   // public static int getSum(int num3, int num4) { //the sum will be stored in this variable int sum;   //add num1 and num2 and the result is stored in sum sum = num3 + num4;   //send back the sum return sum; }   //Show the result public static void showResult(int sum) { System.out.println("The sum of the two numbers is " + sum); System.out.println("Thank you"); }```

Actually, I'm not sure if you're trying to get the same sum that you got in method getSum(int i, int j) and showing it in show result. If so, you'll need to have the two values that you gave it as parameters when you put, getSum(), which you should do if the sum you're passing it is supposed to be the same sum that you're returning from getSum().

If so, you could do this:
Code java:

```public static void showResult(int num1, int num2) { System.out.println("The sum of " + num1 + " and " + num2 + " is " + getSum(num1,num2)); System.out.println("Thank you"); }```