java语言程序设计 第十二章(12.7、12.8、12.9)
2017-10-23 09:47
399 查看
程序小白,希望和大家多交流,共同学习
import java.util.Scanner; public class BinaryToDex { public static void main(String [] args) { Scanner input = new Scanner(System.in); try { System.out.print("Enter a binary string: "); String str = input.next(); System.out.println("The binary value for decimal number " + str + " is " + bin2Dex(str)); } catch (NumberFormatException ex) { System.out.println(ex.getMessage() + " not a decimal string"); } } public static int bin2Dex(String str) throws NumberFormatException { int result = 0; for (int i = str.length() - 1; i >= 0; i--) { if (!(str.charAt(i) == '0' || str.charAt(i) == '1')) { throw new NumberFormatException(str); } else result += Math.pow(2, (str.length() - i - 1)) * (str.charAt(i) - '0'); } return result; } }
public class HexFormatException extends Exception { String hexString; public HexFormatException(String hexString) { super("HexFormatException: " + hexString); this.hexString = hexString; } public String getHexString() { return hexString; } }
import java.util.Scanner; public class HexToDecUseHexFormatException { String hexString; public HexToDecUseHexFormatException(String hexString) throws HexFormatException { setHexString(hexString); } public void setHexString(String hexString) throws HexFormatException { for (int i = 0; i < hexString.length(); i++) { if (judge(hexString.charAt(i)) == -1) { throw new HexFormatException(hexString + " is not a hex string"); } } this.hexString = hexString; } public String getHexString() { return hexString; } private int judge(char ch) { if ((ch >= '0') && (ch <= '9')) { return (ch - '0'); } else if ((ch >= 'A') && (ch <= 'F')) { return (ch - 'A' + 10); } else return -1; } public int hexToDec() { int result = 0; for (int i = hexString.length() - 1; i >= 0; i--) { result += judge(hexString.charAt(i)) * (Math.pow(16, (hexString.length() - i - 1))); } return result; } }
import java.util.Scanner; public class TestHexFormatException { public static void main(String [] args) { Scanner input = new Scanner(System.in); try { System.out.print("Enter a hex string: "); HexToDecUseHexFormatException hex = new HexToDecUseHexFormatException(input.next()); System.out.println("The decimal value for hex number " + hex.getHexString() + " is " + hex.hexToDec()); } catch (HexFormatException ex) { System.out.println(ex.getMessage()); } } }
public class BinaryFormatException extends Exception { String binString; public BinaryFormatException(String binString) { super(binString); this.binString = binString; } public String getBinString() { return binString; } }
public class Bin2DecUseBinaryFormatException { String binString; public Bin2DecUseBinaryFormatException(String binString) throws BinaryFormatException { setBinString(binString); } public void setBinString(String binString) throws BinaryFormatException { for (int i = 0; i < binString.length(); i++) { if ((binString.charAt(i) != '0') && (binString.charAt(i) != '1')) { throw new BinaryFormatException(binString + " is not a binary string"); } } this.binString = binString; } public String getBinString() { return binString; } public int bin2Dec() { int result = 0; for (int i = binString.length() - 1; i >= 0; i--) { result += (binString.charAt(i) - '0') * Math.pow(2, binString.length() - i - 1); } return result; } }
import java.util.Scanner; public class TestBin2DecUseBinaryFormatException { public static void main(String [] args) { Scanner input = new Scanner(System.in); try { System.out.print("Enter a bin string: "); Bin2DecUseBinaryFormatException bin = new Bin2DecUseBinaryFormatException(input.next()); System.out.println("The binary value for decimal number " + bin.getBinString() + " is " + bin.bin2Dec()); } catch (BinaryFormatException ex) { System.out.println(ex.getMessage()); } } }
相关文章推荐
- java语言程序设计 第十二章 (12.23、12.24、12.25、12.26)
- java语言程序设计 第十二章 (12.28、12.30、12.33)
- java语言程序设计 第十二章 (12.11、12.12、12.13、12.14)
- java语言程序设计 第十二章 (12.15、12.16、12.17)
- Java 语言程序设计基础篇原书第八版_第十二章_第八题_程序分享
- java 语言程序设计 第十二章(12.1、12.2、12.3、12.4、12.5、12.6)
- java语言程序设计 第十二章 (12.18、12.19、12.20、12.21、12.22)
- 《程序设计入门—Java语言.翁恺》第七周编程作业(2)-完数
- Java 语言程序设计课件【1~7章】(Word版)
- 《java 语言程序设计》第3、4章编程练习
- Java语言程序设计-基础篇-第八版-编程练习题-第二章
- 【JAVA语言程序设计基础篇】--图形用户界面基础--一些总结
- java语言程序设计第十版(Introduce to java 10th) 课后习题 chapter7-33
- Java语言程序设计-进阶篇(五)算法基础
- Java语言程序设计案例教程--源代码
- java语言程序程序设计 第十一章 (11.11、11.12、11.13、11.14、11.15、11.16、11.17)
- Java语言程序设计-基础篇(四)数组
- chapter1(JAVA语言程序设计)
- Java语言程序设计-基础篇第七章-二维数组
- Java语言程序设计-基础篇(六)正则表达式