In java please, Problem 1 Write a new program RomanNumeralslI 1 and 10 (inclusi
ID: 3728997 • Letter: I
Question
In java please,
Explanation / Answer
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
class roman{
public static String RomanNumerals(int Int) {
LinkedHashMap<String, Integer> roman_numerals = new LinkedHashMap<String, Integer>();
roman_numerals.put("M", 1000);
roman_numerals.put("CM", 900);
roman_numerals.put("D", 500);
roman_numerals.put("CD", 400);
roman_numerals.put("C", 100);
roman_numerals.put("XC", 90);
roman_numerals.put("L", 50);
roman_numerals.put("XL", 40);
roman_numerals.put("X", 10);
roman_numerals.put("IX", 9);
roman_numerals.put("V", 5);
roman_numerals.put("IV", 4);
roman_numerals.put("I", 1);
String res = "";
for(Map.Entry<String, Integer> entry : roman_numerals.entrySet()){
int matches = Int/entry.getValue();
res += repeat(entry.getKey(), matches);
Int = Int % entry.getValue();
}
return res;
}
public static String repeat(String s, int n) {
if(s == null) {
return null;
}
final StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++) {
sb.append(s);
}
return sb.toString();
}
public static void main(String args[]){
int c=0;
while(c==0){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer");
int myint = keyboard.nextInt();
if(myint>0 && myint<10){
System.out.println("The roman number for "+myint+" is: "+RomanNumerals(myint));
c=1;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.