What's new

Closed Help naman pooo convert numbers to numerals 1 - 3000

Status
Not open for further replies.

Heks

Forum Guru
Established
Joined
Dec 9, 2017
Posts
3,364
Solutions
1
Reaction
754
Points
1,059
so ganito po sya, kelangan kung anong number ung nainput ma convert sya sa Roman Numerals (1 hanggang 3000)
may code na ako dito pero hanggang 99 lng kaya iconvert. tens palang kaya help naman po. salamat


System.out.print("Please enter a number between 1 and 3999: ");
int number= scan.nextInt();
String numberString="";


switch ((number%100)/10)
{
case 1: numberString += "X";
break;
case 2: numberString += "XX";
break;
case 3: numberString += "XXX";
break;
case 4: numberString += "XL";
break;
case 5: numberString += "L";
break;
case 6: numberString += "LX";
break;
case 7: numberString += "LXX";
break;
case 8: numberString += "LXXX";
break;
case 9: numberString += "XC";
break;
default: numberString = "Invalid nummber";
break;
}
switch (number%10)
{
case 1: numberString += "I";
break;
case 2: numberString += "II";
break;
case 3: numberString += "III";
break;
case 4: numberString += "IV";
break;
case 5: numberString += "V";
break;
case 6: numberString += "VI";
break;
case 7: numberString += "VII";
break;
case 8: numberString += "VIII";
break;
case 9: numberString += "IX";
break;
default: numberString = "Invalid nummber";
break;
}
System.out.println(numberString);
}
}
 
Kailangan mo ng mas structured na code kesa dyan.

Here's the step by step:

Step 1) User input an integer

Step2) program check input if valid

Step3) if valid, program check input if it belongs to ONES group, TENS group, HUNDRED or THOUSANDS group
....examples:
5 belongs to Ones group (...or 1 or 2 or 3...4...5..single digit)
56 belongs to Tens group (or 11....27.....444....double digit)
512 belongs to Hundreds group ( 3 digits )
2500 belongs to Thousands group (4 digits )

Step 3b) once determined which GROUP, perform the group METHOD
....examples:
void processOnes(int ones)
...1. get the value from an ARRAY of pre-made ones with values

void processTens(int tens)
...1. get the value from an ARRAY of pre-made tens with values

Take note once you get to HUNDREDS method, you might need to invoke (or re-use) ones and tens methods.
And also pagdating sa THOUSANDS method, puwede mong i-re-use and hundreds/tens/ones methods.

Lastly, gawa ka ng HashMap (key=value pair) if you can para sa mga pre-made values. Halimbawa, 1=I, 2=II, 10=X....

I will try to post a sample code here pag may time.
 
Heto, mabilisan ko lang ginawa but I think it's on the right track to the solution.
Iniwan ko yung kalahati ng problema para sa iyo.
Code:
import java.util.HashMap;
import java.util.Map;

public class MainApp {
    private String[] onesArray = {"I","II","III","IV","V","VI","VII","VIII","IX"};
    private String[] tensArray = {"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
    private String[] hundredsArray = {"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
    private String[] thousandsArray = {"M","MM","MMM","MV"};
  
  
    private Map<Integer,String> onesMap = new HashMap<>();
    private Map<Integer,String> tensMap = new HashMap<>();
    private Map<Integer,String> hundredsMap = new HashMap<>();
    private Map<Integer,String> thousandsMap = new HashMap<>();

  
    public static void main(String[] args) {
        MainApp app = new MainApp();
        app.processOnes(2);
        app.processTens(40);
        app.processHundreds(300);
        app.processThousands(2000);
        /**
        * Solved na yung kalahati ng problem mo with this sample.
        * Ang next question (assignment mo) is:
        * What about the numbers in between the tens, hundreds, thousands?????
        * Halimbawa: 45, 13, 101, 1010, etc etc.?????
        *
        * Hint: verify how long is the integer (if it's ones, tens, hundreds or thousands)...do some logic...re-use the methods.
        * Goodluck
        *
        */
    }
  
  
    public MainApp() {
        prepareMaps();
    }
  
    private void prepareMaps() {
        //populates onesMap
        for(int i=0; i<onesArray.length; i++) {
            onesMap.put( (i+1) , onesArray[i]);
        }
        //populates tensMap
        for(int i=0; i<tensArray.length; i++) {
            tensMap.put( (i+1)*10, tensArray[i]);
        }
        //populates hundredsMap
        for(int i=0; i<hundredsArray.length; i++) {
            hundredsMap.put( (i+1)*100, tensArray[i]);
        }
        //populates thousandsMap
        for(int i=0; i<thousandsArray.length; i++) {
            thousandsMap.put( (i+1)*1000, thousandsArray[i]);
        }
    }
      
    public void processOnes(int ones) {
        System.out.println(onesMap.get(ones));
    }
  
    public void processTens(int tens) {
        System.out.println(tensMap.get(tens));
    }
  
    public void processHundreds(int hundreds) {
        System.out.println(hundredsMap.get(hundreds));
    }

    public void processThousands(int thousands) {
        System.out.println(thousandsMap.get(thousands));
    }
  
    //not used anywhere yet
    private void checkInput(int input) {
        if(input > 0 && input < 10) {
            System.out.println("This is ones.");
        } else if(input > 9 && input < 100) {
            System.out.println("This is tens.");
        } else if(input > 99 && input < 1000) {
            System.out.println("This is hundreds.");
        } else if(input > 999 && input < 10000) {
            System.out.println("This is thousands.");
        } else {
            System.out.println("Number is over the limit");
        }
    }

}
 
Heto, mabilisan ko lang ginawa but I think it's on the right track to the solution.
Iniwan ko yung kalahati ng problema para sa iyo.
Code:
import java.util.HashMap;
import java.util.Map;

public class MainApp {
    private String[] onesArray = {"I","II","III","IV","V","VI","VII","VIII","IX"};
    private String[] tensArray = {"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
    private String[] hundredsArray = {"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
    private String[] thousandsArray = {"M","MM","MMM","MV"};
 
 
    private Map<Integer,String> onesMap = new HashMap<>();
    private Map<Integer,String> tensMap = new HashMap<>();
    private Map<Integer,String> hundredsMap = new HashMap<>();
    private Map<Integer,String> thousandsMap = new HashMap<>();

 
    public static void main(String[] args) {
        MainApp app = new MainApp();
        app.processOnes(2);
        app.processTens(40);
        app.processHundreds(300);
        app.processThousands(2000);
        /**
        * Solved na yung kalahati ng problem mo with this sample.
        * Ang next question (assignment mo) is:
        * What about the numbers in between the tens, hundreds, thousands?????
        * Halimbawa: 45, 13, 101, 1010, etc etc.?????
        *
        * Hint: verify how long is the integer (if it's ones, tens, hundreds or thousands)...do some logic...re-use the methods.
        * Goodluck
        *
        */
    }
 
 
    public MainApp() {
        prepareMaps();
    }
 
    private void prepareMaps() {
        //populates onesMap
        for(int i=0; i<onesArray.length; i++) {
            onesMap.put( (i+1) , onesArray[i]);
        }
        //populates tensMap
        for(int i=0; i<tensArray.length; i++) {
            tensMap.put( (i+1)*10, tensArray[i]);
        }
        //populates hundredsMap
        for(int i=0; i<hundredsArray.length; i++) {
            hundredsMap.put( (i+1)*100, tensArray[i]);
        }
        //populates thousandsMap
        for(int i=0; i<thousandsArray.length; i++) {
            thousandsMap.put( (i+1)*1000, thousandsArray[i]);
        }
    }
     
    public void processOnes(int ones) {
        System.out.println(onesMap.get(ones));
    }
 
    public void processTens(int tens) {
        System.out.println(tensMap.get(tens));
    }
 
    public void processHundreds(int hundreds) {
        System.out.println(hundredsMap.get(hundreds));
    }

    public void processThousands(int thousands) {
        System.out.println(thousandsMap.get(thousands));
    }
 
    //not used anywhere yet
    private void checkInput(int input) {
        if(input > 0 && input < 10) {
            System.out.println("This is ones.");
        } else if(input > 9 && input < 100) {
            System.out.println("This is tens.");
        } else if(input > 99 && input < 1000) {
            System.out.println("This is hundreds.");
        } else if(input > 999 && input < 10000) {
            System.out.println("This is thousands.");
        } else {
            System.out.println("Number is over the limit");
        }
    }

}
Wow. Thank you very much for your effort. Pero d kami allowed gumamit ng ibang structure bukod sa if else at switch case lng poo, thats the challenge for us, all though i know how to use arrays.

Anyways thankyou very muchh :)))
 
Im sorry hindi ko na indicate sa post ko na if else at switch case lng pala gagamitin sorry po :(
 
Im sorry hindi ko na indicate sa post ko na if else at switch case lng pala gagamitin sorry po :(
no worries, Shen Yue . Oks lang.
Mas maraming lines of codes at ang mangyayari ay magiging PROCEDURAL ang kalalabasan ng program mo but it will be ok as a solution.

You can get the idea from this example and adapt it.
Bale...first evaluate....determine group....process. Ganon lang.
 
no worries, Shen Yue . Oks lang.
Mas maraming lines of codes at ang mangyayari ay magiging PROCEDURAL ang kalalabasan ng program mo but it will be ok as a solution.

You can get the idea from this example and adapt it.
Bale...first evaluate....determine group....process. Ganon lang.
Thank you very much ,you're really helpfulhelpful .Gob bless :))
 
Status
Not open for further replies.

Similar threads

Back
Top