Java语言程序设计与数据结构(基础篇)课后练习题 第十二章

public static void main(String[] args) {

// TODO Auto-generated method stub

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.hex2Dec());

} catch (HexFormatException ex) {

System.out.println(ex.getMessage());

}

}

}

class HexFormatException extends Exception {

String hexString;

public HexFormatException(String hexString) {

super("HexFormatException: " + hexString);

this.hexString = hexString;

}

public String getHexString() {

return hexString;

}

}

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 hex2Dec() {

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;

}

}

12.9

=================================================================

import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);

System.out.print("Enter a binary string: ");

String s = input.next();

try {

int i = bin2Dec(s);

System.out.print(i);

} catch (NumberFormatException e) {

System.out.print(“Wrong!\n” + e);

System.exit(1);

}

}

public static int bin2Dec(String hex) throws BinaryFormatException {

int value = 0;

for (int i = 0; i < hex.length(); i++) {

char hexChar = hex.charAt(i);

if (hexChar == ‘0’ || hexChar == ‘1’) {

value = value * 2 + binChar2Dec(hexChar);

} else {

throw new NumberFormatException(“Not a binstring!”);

}

}

return value;

}

public static int binChar2Dec(char ch) {

return ch - ‘0’;

}

}

class BinaryFormatException extends NumberFormatException {

public BinaryFormatException(String s) {

super(s);

}

}

12.10

==================================================================

运行时间很长!!!!!!!,等结果的话要耐心点。。。

import java.util.ArrayList;

public class dishierzhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

ArrayList list = new ArrayList();

double i = 123456;

while(true){

i++;

try{

list.add(i);

} catch (OutOfMemoryError e) {

// TODO: handle exception

System.out.print(“Wrong!\n” + e);

System.exit(1);

}

}

}

}

12.11

==================================================================

import java.io.File;

import java.io.PrintWriter;

import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

if (args.length != 2) {

System.out.println(“Wrong!”);

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
img

前端面试题汇总

JavaScript

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

性能

linux

前端资料汇总

cript**

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

性能

linux

前端资料汇总

  • 15
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值