From c188fa9187d6cfbcc05530977cfee266bb4fe338 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Thu, 20 Jun 2019 17:23:49 +0800 Subject: [PATCH] Update IntegerToRoman.java --- Conversions/IntegerToRoman.java | 44 ++++++++++++++++----------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/Conversions/IntegerToRoman.java b/Conversions/IntegerToRoman.java index b23a002e..e979eb53 100644 --- a/Conversions/IntegerToRoman.java +++ b/Conversions/IntegerToRoman.java @@ -1,29 +1,29 @@ package Conversions; public class IntegerToRoman { - private static int[] allArabianRomanNumbers = new int[] {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; - private static String[] allRomanNumbers = new String[] {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; - + private static int[] allArabianRomanNumbers = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; + private static String[] allRomanNumbers = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; + public static String integerToRoman(int num) { - if(num <= 0) { - return ""; - } - - StringBuilder builder = new StringBuilder(); - - for(int a = 0;a < allArabianRomanNumbers.length;a++) { - int times = num / allArabianRomanNumbers[a]; - for(int b = 0;b < times;b++) { - builder.append(allRomanNumbers[a]); - } - - num -= times * allArabianRomanNumbers[a]; - } - - return builder.toString(); + if (num <= 0) { + return ""; + } + + StringBuilder builder = new StringBuilder(); + + for (int a = 0; a < allArabianRomanNumbers.length; a++) { + int times = num / allArabianRomanNumbers[a]; + for (int b = 0; b < times; b++) { + builder.append(allRomanNumbers[a]); + } + + num -= times * allArabianRomanNumbers[a]; + } + + return builder.toString(); } - + public static void main(String[] args) { - System.out.println(IntegerToRoman.integerToRoman(2131)); - } + System.out.println(IntegerToRoman.integerToRoman(2131)); + } }