Results 1 to 3 of 3
  1. #1
    syntrax is offline Member
    Join Date
    Sep 2009
    Location
    http://www.kouje.com
    Posts
    16
    Rep Power
    0

    Angry THIS PROGRAM IS DRIVING ME CRAZY!!! help fixing it

    Ok, so.. This program is using a recursive method. but every time it goes into the loop-thing-that's-not-a-loop, it ALWAYS goes into the else statement NO MATTER wat way the "< >" sign i choose... i'm going nuts and i only have 2 hrs to turn in the program >.<

    Java Code:
    import java.util.Scanner;
    public class assgnMain15
    {
    	public static void main(String args[])
    	{
    		int number1, num3, number2;
    		Scanner kb = new Scanner(System.in);
    		
    		System.out.print("Enter number: ");
    		number1 = kb.nextInt();
    		
    		System.out.print("Enter exponential number: ");
    		number2 = kb.nextInt();
    		
    		num3 = assgn15.message(number1, number2);
    		System.out.println("Number = " + num3);
    	}
    }
    i know that here's where the problem is, but i really dont know how to fix it and make it stop giving me 1,,, ALWAYS!!!

    it always goes into the "else" statement.
    Java Code:
    public class assgn15
    {
    	public static int message(int num1, int num2)
    	{
    		if(num2 > 0)
    		{
    			return message (num1, num2 -1);
    		}
    		else
    			return 1;
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're wrong. The if portion of your if/else block is called; however your if block doesn't do anything to change the return value to anything but one, so you'll never know that it is called.

    To prove this to yourself, change your assgn15 to:
    Java Code:
    public class assgn15
    {
    	public static int message(int num1, int num2)
    	{
    		if(num2 > 0)
    		{
    			System.out.println("num2 > 0 is true");
    			return message (num1, num2 -1);
    		}
    		else
    			return 1;
    	}
    }
    Solution: have your if block do more than simply return message(num1, num2-1). Maybe you need to multply something here. ;)
    Last edited by Fubarable; 12-18-2009 at 04:33 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The method message(int,int) returns 1. Always. That's what it does. It is a round about way of writing "return 1;".

    To see why that is, pick a couple of small numbers and trace through the code seeing which line is executed at each step.

    If you want the method to do something else you will have to change it.

Similar Threads

  1. Replies: 1
    Last Post: 08-27-2009, 01:20 PM
  2. [SOLVED] I need help fixing an error
    By leonardjr in forum New To Java
    Replies: 8
    Last Post: 02-21-2009, 08:33 PM
  3. help with fixing my assignment.please...error
    By Jin-enigma-PL in forum New To Java
    Replies: 4
    Last Post: 02-05-2009, 12:12 AM
  4. Errors driving me crazy! although compiles fine
    By irishsea2828 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:23 PM
  5. Exception Error need help fixing
    By skinnybones in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •