Results 1 to 3 of 3
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default recursion problem

    Hi I have this problem with a recursion method which supposed to convert numbers into roman numerals.

    I am giving a number, and the num variable I wrote should change by the length of the number, so if it's a 4digit number the given number will be divided by 1000 ,3digits /100.. .
    From some reason it does that for the first time and then the number stay's the same.

    So my output for 4321 is: MMMMCCCCXXXXIIII.
    Any ideas what could be the problem?

    Here's part of the code (one method out of two):

    public static String num2Roms (int a){
    String numstr = (String) ""+a;
    int digs = numstr.length()-1; // set the length of the number.
    int num;
    switch (digs) {
    case 0: num = a/1; break;
    case 1: num = a/10; break;
    case 2: num = a/100; break;
    default:num = a/1000; break;
    }
    if (a == 0) //base case.
    return "";
    else {
    return converter (digs, num, a);
    }
    }

    //thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    When posting code, make sure it's formatting using the CODE tags. It should also be in the form of an SSCCE.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default I think it's got to do with

    the switch statement, it doesn't change the number..

Similar Threads

  1. Problem with case - might need recursion
    By Angelar in forum New To Java
    Replies: 6
    Last Post: 10-13-2010, 02:25 PM
  2. Recursion problem
    By luke in forum New To Java
    Replies: 6
    Last Post: 10-06-2010, 06:35 AM
  3. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  4. recursion and call stack problem
    By OptimusPrime in forum New To Java
    Replies: 4
    Last Post: 12-26-2009, 09:49 PM
  5. Java Recursion Problem
    By gmnnn in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-06-2009, 04:22 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
  •