+ 1

How Can I convert a random number to a roman number ?

27th Sep 2016, 4:21 AM
AMMA
AMMA - avatar
4 Réponses
+ 3
https://code.sololearn.com/c2Dbb9j84rMw public class Program { public static void printRoman(int n) { if (n > 0) { while (n >= 1000) { System.out.print("M"); n -= 1000; } if (n >= 900) { System.out.print("CM"); n -= 500; } if (n >= 500) { System.out.print("D"); n -= 500; } if (n >= 400) { System.out.print("CD"); n -= 400; } while (n >= 100) { System.out.print("C"); n -= 100; } if (n >= 90) { System.out.print("XC"); n -= 90; } if (n >= 50) { System.out.print("L"); n -= 50; } if (n >= 40) { System.out.print("XL"); n -= 40; } while (n >= 10) { System.out.print("X"); n -= 10; } if (n >= 9) { System.out.print("IX"); n -= 9; } if (n >= 5) { System.out.print("V"); n -= 5; } if (n >= 4) { System.out.print("IV"); n -= 4; } while (n >= 1) { System.out.print("I"); n -= 10; } System.out.println(); } } public static void main(String[] args) { printRoman(42); printRoman(145); printRoman(3649); } }
27th Sep 2016, 2:39 PM
Zen
Zen - avatar
+ 2
First you need to import Random. Then you can generate you random number... Random r = new Random(); int k = r.nextInt(3)+1; if(k == 1){ System.out.println(I); }else if(k == 2) { System.out.println(II); }else { System.out.println(III); } How I helped you :D
27th Sep 2016, 6:27 AM
MM1132
MM1132 - avatar
+ 1
Thanks lol :D
27th Sep 2016, 3:09 PM
MM1132
MM1132 - avatar
+ 1
Thanks!
27th Sep 2016, 3:17 PM
AMMA
AMMA - avatar