Input and output of base numbers

Write a program that reads an integer and outputs it in binary, octal, and hexadecimal, and outputs the reciprocal in hexadecimal floating point.

public class test1 {
    
    
    public static void main(String[] args) {
    
    
        //写个程序, 它读取一个整数并以二进制, 八进制, 和十六进制输出, 以十六进制浮点数输出倒数
        Scanner input = new Scanner(System.in);
        int a;
        a = input.nextInt();
        System.out.println(Integer.toString(a,2));
        System.out.println(Integer.toString(a,8));
        System.out.println(Integer.toString(a,16));

        System.out.println(Double.toHexString(1.0/a));
    }
}

There are two methods. The first method is that we use this Interger. ToStirng. But this method can only be used to deal with integer types. For this floating point type, there is no good way to deal with it. We can use this
toBinaryString, toOctalStringand toHexStringthree There is a built-in method in the packaging class for conversion.
If you want to read binary and octal hexadecimal, we can use this parseint to convert. However, although this conversion can achieve the goal, it does not work when you enter illegal numbers. It is easy to process, for example, when you enter octal, you enter 9

import java.util.Scanner;

public class NumberReader {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);

        // 读取八进制数字
        System.out.print("请输入一个八进制数字:");
        String octalInput = scanner.next();
        int octalNumber = Integer.parseInt(octalInput, 8);
        System.out.println("读取的八进制数字为:" + octalNumber);

        // 读取二进制数字
        System.out.print("请输入一个二进制数字:");
        String binaryInput = scanner.next();
        int binaryNumber = Integer.parseInt(binaryInput, 2);
        System.out.println("读取的二进制数字为:" + binaryNumber);

        // 读取十六进制数字
        System.out.print("请输入一个十六进制数字:");
        String hexInput = scanner.next();
        int hexNumber = Integer.parseInt(hexInput, 16);
        System.out.println("读取的十六进制数字为:" + hexNumber);
    }
}

In Java, there is no direct built-in method to directly read binary and octal numbers. However, you can use regular expressions to verify that the input is a valid binary or octal number and convert accordingly. When reading illegal characters, you can throw an exception to handle it.
The following is a sample program that demonstrates how to use regular expressions for validation and exception handling:

import java.util.Scanner;

public class NumberReader {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);

        // 读取八进制数字
        System.out.print("请输入一个八进制数字:");
        String octalInput = scanner.next();
        try {
    
    
            int octalNumber = readOctalNumber(octalInput);
            System.out.println("读取的八进制数字为:" + octalNumber);
        } catch (IllegalArgumentException e) {
    
    
            System.out.println("输入的不是有效的八进制数字。");
        }

        // 读取二进制数字
        System.out.print("请输入一个二进制数字:");
        String binaryInput = scanner.next();
        try {
    
    
            int binaryNumber = readBinaryNumber(binaryInput);
            System.out.println("读取的二进制数字为:" + binaryNumber);
        } catch (IllegalArgumentException e) {
    
    
            System.out.println("输入的不是有效的二进制数字。");
        }
    }

    // 读取并验证八进制数字
    private static int readOctalNumber(String input) {
    
    
        if (!input.matches("[0-7]+")) {
    
    
            throw new IllegalArgumentException();
        }
        return Integer.parseInt(input, 8);
    }

    // 读取并验证二进制数字
    private static int readBinaryNumber(String input) {
    
    
        if (!input.matches("[01]+")) {
    
    
            throw new IllegalArgumentException();
        }
        return Integer.parseInt(input, 2);
    }
}

In this example, we use matches()the method to check if the input string matches a specified regular expression. If the string is not a valid octal or binary number, IllegalArgumentExceptionan exception is thrown. Otherwise, we convert the string to an integer for output.

In addition to the first time, we can also use formatting symbols to output

public static void exe1(int n) {
    
    
    	System.out.printf("十进制输出%d\n",n);
		System.out.printf("二进制输出"+Integer.toBinaryString(n)+"\n");
		System.out.printf("十六进制输出"+"%x\n",n);
		System.out.printf("八进制输出%o\n",n);
}

Guess you like

Origin blog.csdn.net/everything_study/article/details/132471348