Add H3Q1.java
This commit is contained in:
56
H3Q1.java
Normal file
56
H3Q1.java
Normal file
@@ -0,0 +1,56 @@
|
||||
// Question 1
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
// My logic here. We cant have 100, so C is disregarded. Only care about L,X,V,I
|
||||
String create_roman(int input) {
|
||||
|
||||
|
||||
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int input;
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
do {
|
||||
System.out.print("Enter an integer between 1 and 90, inclusively: ");
|
||||
input = scanner.nextInt();
|
||||
} while (input < 1 || input > 90); // Ask for input again if not in range of [1,90]
|
||||
|
||||
System.out.println(input);
|
||||
|
||||
int times = 0;
|
||||
|
||||
Map<Integer, String> test = new HashMap<>();
|
||||
test.put(100, "C");
|
||||
test.put(90, "XC");
|
||||
test.put(50, "L");
|
||||
test.put(40, "XL");
|
||||
test.put(10, "X");
|
||||
test.put(9, "IX");
|
||||
test.put(5, "V");
|
||||
test.put(4, "IV");
|
||||
test.put(1, "I");
|
||||
|
||||
int convert = input;
|
||||
|
||||
String s = "";
|
||||
|
||||
for(var x : test.entrySet()) {
|
||||
times = convert / x.getKey();
|
||||
convert %= x.getKey();
|
||||
while(times-- > 0) {
|
||||
s = s.concat(x.getValue());
|
||||
}
|
||||
}
|
||||
System.out.println("Roman: " + s);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user