题目 1892: 蓝桥杯-P0102

时间:2024-03-09 17:18:48

题目描述:

用户输入三个字符,每个字符取值范围是0-9,A-F。然后程序会把这三个字符转化为相应的十六进制整数,并分别以十六进制,十进制,八进制输出,十六进制表示成3位,八进制表示成4位,若不够前面补0。(不考虑输入不合法的情况)

代码:

package lanqiao;

import java.math.BigInteger;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        int value = Integer.parseInt(s,16);
        System.out.println("Hex: 0x"+s);
        System.out.println("Decimal: "+value);
        System.out.print("Octal: ");
        if (Integer.toOctalString(value).length()<4) {
            for (int i = 0; i < 4-Integer.toOctalString(value).length(); i++) {
                System.out.print("0");
            }
        }
        System.out.print(Integer.toOctalString(value));
    }
}