-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.java
More file actions
47 lines (39 loc) · 1.43 KB
/
Convert.java
File metadata and controls
47 lines (39 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Scanner;
public class Convert {
public static void main(String[] args) {
Scanner ent = new Scanner(System.in);
String str1 = new String();
String[] str2;
String c, bin, hex;
int count1 = 0;
int count2 = 0;
long dec;
c = ent.nextLine();
count1 = Integer.parseInt(c);
while (count1 > 0) {
str1 = ent.nextLine();
count2++;
str2 = str1.split(" ");
if (str2[1].equals("bin")) {
dec = Long.parseLong(str2[0], 2);
System.out.println("Case " + count2 + ":" + "\n" + dec + " dec");
hex = Long.toHexString(dec);
System.out.println(hex + " hex");
} else if (str2[1].equals("dec")) {
dec = Long.parseLong(str2[0]);
hex = Long.toHexString(dec);
System.out.println("Case " + count2 + ":" + "\n" + hex + " hex");
bin = Long.toBinaryString(dec);
System.out.println(bin + " bin");
} else if (str2[1].equals("hex")) {
dec = Long.parseLong(str2[0], 16);
System.out.println("Case " + count2 + ":" + "\n" + dec + " dec");
bin = Long.toBinaryString(dec);
System.out.println(bin + " bin");
}
System.out.printf("\n");
count1--;
}
ent.close();
}
}