华为机试练习---进制数的相互转换

package huawei;

import java.util.Scanner;
import java.util.Stack;

/**
 * 进制数的相互转换:decimal、octal、hexadecimal、binary
 * 注意问题:
 * 1、在静态方法内部创建对象引用,否则提示:
 * Cannot make a static reference to the non-static method push(Object) from the type Stack
 * 2、Queue类是接口,不能实例化,通过Queue类引用来引用LinkedList对象,否则提示:
 * Cannot instantiate the type Queue<String>
 * 3、Integer.parseInt(char ch)转化的是字符ch的ASCII码值的整数值,而不是ch的字面值,所以必须要转换为字符串String类型
 * 
 * @author USER
 *
 */
public class BaseConvert {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int decimal = in.nextInt();
		String hexa = in.next();
		String octal = in.next();
		String binary = in.next();
		//10->8
		decimalToOctal(decimal);
		System.out.println();
		//10->2
		decimalToBinary(decimal);
		System.out.println();
		//10->16
		decimalToHexadecimal(decimal);
		System.out.println();
		//16->10
		hexadecimalToDecimal(hexa);
		//8->10
		octalToDecimal(octal);
		//2->10
		binaryToDecimal(binary);
	}

	private static void binaryToDecimal(String binary) {
		// TODO Auto-generated method stub
		int len = binary.length();
		int sumBinary = 0;
		for (int i = 0; i < len; i++) {
			sumBinary += Integer.parseInt(binary.charAt(i)+"")*Math.pow(2, len-1-i);	 
		}
		System.out.println(sumBinary);
	}

	private static void octalToDecimal(String octal) {
		// TODO Auto-generated method stub
		int len = octal.length();
		int sumOctal = 0;
		for (int i = 0; i < len; i++) {
			sumOctal += Integer.parseInt(octal.charAt(i)+"")*Math.pow(8, len-1-i);	 
		}
		System.out.println(sumOctal);
	}

	private static void hexadecimalToDecimal(String hexa) {
		// TODO Auto-generated method stub
		int sum = 0;
		int len = hexa.length();
		for (int i = 0; i < len; i++) {
			switch (hexa.charAt(i)) {
			case 'A':
				sum += 10*Math.pow(16, len-1-i);
				break;
			case 'B':
				sum += 11*Math.pow(16, len-1-i);
				break;
			case 'C':
				sum += 12*Math.pow(16, len-1-i);
				break;
			case 'D':
				sum += 13*Math.pow(16, len-1-i);
				break;
			case 'E':
				sum += 14*Math.pow(16, len-1-i);
				break;
			case 'F':
				sum += 15*Math.pow(16, len-1-i);
				break;
			default:
				sum += Integer.parseInt(hexa.charAt(i)+"")*Math.pow(16, len-1-i);
				break;
			} 
		}
		System.out.println(sum);
	}

	private static void decimalToHexadecimal(int decimal) {
		// TODO Auto-generated method stub
		Stack<String> stack = new Stack<String>();
		while (decimal > 0) {
			int temp = decimal % 16;
			switch (temp) {
			case 10:
				stack.push("A");
				break;
			case 11:
				stack.push("B");
				break;
			case 12:
				stack.push("C");
				break;
			case 13:
				stack.push("D");
				break;
			case 14:
				stack.push("E");
				break;
			case 15:
				stack.push("F");
				break;
			default:
				stack.push(Integer.toString(temp));
				break;
			}
			decimal = decimal / 16;
		}
		while (!stack.isEmpty()) {
			System.out.print(stack.pop());
		}
	}

	private static void decimalToBinary(int decimal) {
		// TODO Auto-generated method stub
		Stack<Integer> stack = new Stack<Integer>();
		while (decimal > 0) {
			stack.push(decimal % 2);
			decimal = decimal / 2;
		}
		while (!stack.isEmpty()) {
			System.out.print(stack.pop());
		}
	}

	private static void decimalToOctal(int decimal) {
		// TODO Auto-generated method stub
		Stack<Integer> stack = new Stack<Integer>();
		while (decimal > 0) {
			stack.push(decimal % 8);
			decimal = decimal / 8;
		}
		while (!stack.isEmpty()) {
			System.out.print(stack.pop());
		}
	}	
}


其实,对于进制的转换,java已经提供好了相应的方法直接实现,不用自己再写代码实现了。可见java对类的封装性的强大和方便。

package huawei;

import java.util.Scanner;

/**
 * 直接通过Integer类的方法实现进制间的转换,更加简单
 * @author USER
 *
 */
public class BaseConvert1 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		
		//10->2
		System.out.println(Integer.toBinaryString(a));
		//10->8
		System.out.println(Integer.toOctalString(a));
		//10->16
		System.out.println(Integer.toHexString(a));
		//2->10
		String b = in.next();
		System.out.println(Integer.valueOf(b, 2));
		//8->10
		String c = in.next();
		System.out.println(Integer.valueOf(c, 8));
		//16->10
		String d = in.next();
		System.out.println(Integer.valueOf(d, 16));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值