2022.01.17

2022.01.17,第1张

2022.01.17 1. BigInteger 1.1 概述
1、Integer类作为int的包装类,能存储的最大整型值为231-1,Long类也是有限的, 最大为263-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类 都无能为力,更不用说进行运算了。

2、java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供
所有 Java 的基本整数 *** 作符的对应物,并提供 java.lang.Math 的所有相关方法。 另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、 位 *** 作以及一些其他 *** 作。

1.2 常用方法
public BigInteger abs():返回此 BigInteger 的绝对值的 BigInteger。
BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger
BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger
BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger
BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数 相除只保留整数部分。
BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。
BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟(this % val) 的两个 BigInteger 的数组。
BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。

1.3 使用
		// 必须传入字符串
		BigInteger v1 = new BigInteger("123123");
		BigDecimal v2 = new BigDecimal(20);
		BigDecimal v3 = new BigDecimal(20);
		// + 
		BigDecimal result = v2.add(v3);
		System.out.println(result);
		// - 
		result = v2.subtract(v3);
		System.out.println(result);
		// * 
		result = v2.multiply(v3);
		System.out.println(result);
		// / 
		result = v2.divide(v3);
		System.out.println(result);
		// % 
		result = v2.remainder(v3);
		System.out.println(result);
	
1.4 阶乘
	public static void main(String[] args) {
		System.out.println(test(120));
		System.out.println(test1(120));
		System.out.println(Long.MAX_VALUE);
	}
	public static long test(long n){
		long result = 1;
		for (int i = 1; i <= n; i++) {
			result *= i;
		}
		return result;
	}
	
	public static BigInteger test1(long n){
		BigInteger result = new BigInteger("1");
		for (int i = 1; i <= n; i++) {
			result= result.multiply(new BigInteger(i+""));
		}
		return result;
	}

2. Math 2.1 概述
java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回
值类型一般为double型。

2.2 常用方法
abs	绝对值 acos,asin,atan,cos,sin,tan	三角函数  sqrt		平方根
pow(double a,doble b)	a的b次幂  log	自然对数
exp	e为底指数
max(double a,double b)
min(double a,double b)
random()	返回0.0到1.0的随机数
long round(double a)	double型数据a转换为long型(四舍五入)
toDegrees(double angrad) 弧度—>角度
toRadians(double angdeg) 角度—>弧度

2.3 使用方式
		// 绝对值
		System.out.println(Math.abs(-3));
		// 向上取整 , 有小数 就进位
		System.out.println(Math.ceil(2.000000001));
		// 向下取整 , 舍弃小数
		System.out.println(Math.floor(2.999999999999));
		// 返回最大的值
		System.out.println(Math.max(2.3, 4.1));
		// 返回最小的值
		System.out.println(Math.min(2.3, 4.1));
		// 开平方
		System.out.println(Math.sqrt(16));
		// 开立方
		System.out.println(Math.cbrt(8));
		// 返回一个 大于等于0 且小于 1 的随机数
		// 本质就是Random的nextDouble方法
		System.out.println(Math.random());
		// 四舍六入五取偶 , 当小数大于0.5  就进1 , 小于 0.5 就舍弃,当小数为0.5整的时候,取偶数
		// 2.5  = 2 , 3.5 = 4 , 2.50001 = 3 
		System.out.println(Math.rint(2.50001));
		System.out.println(Math.rint(9.5));
		// N的M次幂 ,  2的3次幂
		System.out.println(Math.pow(2, 3));
	
3. 异常机制 3.1 碰到过的异常
1 空指针异常
2 下标越界异常
3 类型转换异常
4 栈内存溢出

3.2 概述
异常是Java中提供的一种识别及响应错误情况的一致性机制。有效地异常处理能使程序更加健壮、易于调试。
异常发生的原因有很多,比如:
1)	用户输入了非法数据
2)	要打开的文件不存在
3)	网络通信时连接中断
4)	JVM内存溢出
5)	这些异常有的是因为用户错误引起,有的是程序错误引起的,还有其它一些是因为物理错误引起的。

 * 异常 其实就是错误的一种说法,在java中 有一个专门模拟所有异常和错误的类 Throwable ,是所有异常类的父类
 * 
 * try...catch... : 处理异常,一般用于客户端
 * 
 * throws : 抛出异常,一般用于服务端
 * 
 * throw : 异常源点
 * 
 * finally : 必须执行的语句
3.3 继承体系

3.4 Error
系统内部错误,这类错误由系统进行处理,程序本身无需捕获处理。 
比如:OOM(内存溢出错误)、VirtualMachineError(虚拟机错误)、StackOverflowError(堆栈溢出错误)等,一般发生这种情况,JVM会选择终止程序。

3.5 Exception 3.5.1 概述
Exception是所有异常类的父类。分为非RuntimeException和RuntimeException 。
·	非RuntimeException 
指程序编译时需要捕获或处理的异常,如IOException、自定义异常等。属于checked异常。
·	RuntimeException 
指程序编译时不需要捕获或处理的异常,如:NullPointerException等。属于unchecked异常。一般是由程序员粗心导致的。如空指针异常、数组越界、类型转换异常等。


3.5.2 常用方法

3.5.3 Try…catch…
 * try{
 * 		高风险代码
 * 		只要出错,就执行catch,try中剩余代码不再执行
 * 		如果没出错,try就可以顺利执行完成,并且catch不再执行
 * }catch(异常类型 变量){
 * 		处理方案;
		try {
			// 加载对应的文件
			FileInputStream fis = new FileInputStream("123.txt");
			System.out.println("=========");
		} catch (Exception e) {
			// 打印错误栈帧和信息,一般用于程序员排错
			e.printStackTrace();
			// 获取错误信息,一般用于反馈给客户端
			String msg = e.getMessage();
			System.out.println(msg);
			System.out.println("xxxxxxxxxx");
		}
		// 不再终止生命周期执行
		System.out.println(1111);
	
 * try{
 * 		高风险代码;
 * }catch(异常类型 变量){
 * 		处理措施;
 * }catch(异常类型 变量){
 * 		处理措施;
 * }catch(异常类型 变量){
 * 		处理措施;
 * }..... 
 * 
 * 可以写多个catch  , 但是 从上往下 必须是从子类到父类,或者没有继承关系
		// 当try下面 可能会出现多个异常的时候,并且每个异常对应的解决方案不同的时候
		// 需要写多个catch进行不同的处理
		// 如果解决方案一致,则只写一个 exception 即可
		try {
			new FileInputStream("123");
			String stre = null;
			System.out.println(stre.trim());
		} catch (FileNotFoundException e) {
			System.out.println("找不到指定文件");
		} catch (IOException e) {
		} catch (NullPointerException e) {
			System.out.println("不能为空");
		} catch (Exception e) {
			System.out.println("其他异常");
		}
	
 * try{
 * 
 * }catch(异常类型1 | 异常类型2 | 异常类型3 ...){
 * 
 * }
 * 
 * 同时捕捉多个异常,并且多个异常类型直接用 | 隔开,并且 不能有继承关系
 * 如果有继承关系 则直接写父类即可
	public static void main(String[] args) {
		try {
			new FileInputStream("123");
		} catch ( FileNotFoundException | NullPointerException e) {
			// TODO: handle exception
		}
	}

3.5.4 Throws
 * throws 抛出异常,该方式并不会处理理解,一种提醒机制,告诉调用人员,这里可能会有错误
 * 
 * 如果 知道如何解决 则使用try 解决问题, 否则就要通过 throws 抛出问题
 * 
 * 只要你的下家提醒了你,要么你解决,要么抛给上家
	public static void main(String[] args) {
		try {
			m1();
		} catch (Exception e) {
			// e.printStackTrace();
			// TODO 解决错误的措施
		}
		System.out.println("====");
	}

	// throws 可以同时抛出多个异常,多个用逗号隔开
	public static void m1() throws FileNotFoundException,Exception {
		m2();
	}

	public static void m2() throws FileNotFoundException {
		m3();
	}

	public static void m3() throws FileNotFoundException {
		new FileInputStream("123");
	}

3.5.5 Finally
 * finally : 必须执行的语句块,不能单独使用,必须和try 或者try..catch..一起使用
 * 
 * finally 只有一种不执行的情况,那就是 System.exit(0);
	public static void main(String[] args) {
		try {
			FileInputStream fis = new FileInputStream("123");
			System.out.println(fis);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			System.out.println("必须执行");
		}
	}

	public static void main(String[] args) {
		int result = m1();
		// 11
		System.out.println(result);
	}

	public static int m1() {
		int i = 10;
		try {
			// 因为finally中有return,所以这里的return 不执行,但是 i++执行,编译之后 会把return去掉
			return i++;
		} catch (Exception e) {
		} finally {
			System.out.println(i + "-------");
			return i;
		}
	}

	public static void main(String[] args) {
		// 提升作用域,否则访问不到
		// 添加默认值,否则因为局部变量没有默认值而报错
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("D://123.txt");
			System.out.println("执行成功");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				// 判断是否打开该资源
				if (fis != null) {
					System.out.println("关闭成功");
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

 * java7新特征,自动关闭资源
 * 
 * try(开启资源语句;){
 * 		高风险代码;
 * }catch(异常类型 变量){
 * 		处理方案;
 * }
	public static void main(String[] args) {
		try(
				FileInputStream fis = new FileInputStream("123");
				) {
			//  *** 作
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

3.5.6 不能有更宽泛的异常
 * 方法覆写,不能比原方法有更宽泛的异常
 * 
 * 子类方法抛出的异常,不能大于父类异常,也就是 小于等于 父类异常均可
 * 			要么和父类抛出的异常一致,要么是父类抛出异常类的子类,或者 直接不抛出异常
public class Exception_10 {
	public static void main(String[] args) {

	}
}
class A {
	public void m1() throws IOException {

	}
}
class B extends A {
	public void m1() throws IOException {

	}
}
3.6 自定义异常类 3.6.1 需求
完成用户登陆功能
传入用户信息,校验用户名和密码是否正确,如果正确则返回该用户所有信息,反之则提示错误信息

3.6.2 实体类
 * 用户模块
public class User {
	private String username;
	private String password;
	private String nickname;

	public User(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password
				+ ", nickname=" + nickname + "]";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public User(String username, String password, String nickname) {
		super();
		this.username = username;
		this.password = password;
		this.nickname = nickname;
	}

	public User() {
		super();
	}

}

3.6.3 异常类
 * 自定义异常类
 * 
 * 1 继承一个已有的异常类
 * 			如果是运行时异常,则继承RuntimeException 反之则继承 Exception即可
 * 			一般情况下,我们写的 一定是编译时异常
 * 2 公共的无参构造
 * 3 公共的有参构造,参数是错误信息,然后通过super 把错误信息传递到父类
public class UserException extends Exception {
	public UserException() {
	}

	public UserException(String msg) {
		super(msg);
	}
}
3.6.4 Controller
public class UserController {
	private static UserService userService = new UserService();

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("请选择功能 : n t 1 注册   2 登陆");
			int value = scanner.nextInt();
			if (value == 1) {
				System.out.println("请输入用户名 : ");
				String username = scanner.next();
				System.out.println("请输入昵称 : ");
				String nickname = scanner.next();
				System.out.println("请输入密码 : ");
				String password = scanner.next();
				System.out.println("再次输入密码 : ");
				String password2 = scanner.next();
				// 校验密码是否一致
				while (!password.equals(password2)) {
					System.out.println("两次密码不一致,请再次输入!");
					System.out.println("请输入密码 : ");
					password = scanner.next();
					System.out.println("再次输入密码 : ");
					password2 = scanner.next();
				}
				// TODO 注册
				User user = new User(username, password, nickname);
				try {
					userService.add(user);
					System.out.println("注册成功 : " + user);
				} catch (UserException e) {
					System.out.println("注册失败 : " + e.getMessage());
				}
			} else if (value == 2) {
				System.out.println("请输入用户名 : ");
				String username = scanner.next();
				System.out.println("请输入密码 : ");
				String password = scanner.next();
				// 封装到对象中
				User inputUser = new User(username, password);
				// TODO 校验登陆
				try {
					User loginUser = userService.login(inputUser);
					System.out.println("登陆成功 : " + loginUser);
				} catch (UserException e) {
					System.out.println("登陆失败 : " + e.getMessage());
				}
			}

		}
	}
}
3.6.5 Data
 * 模拟数据库
 * public class Database {
	private static User[] users = null;
	static {
		users = new User[2];
		users[0] = new User("admin", "root", "管理员");
		users[1] = new User("test", "1234", "测试人员");
	}
	public static User[] getUser(){
		return users;
	}
	public static void add(User user){
		// 创建新数组,长度为原来+1
		User[] newUsers = new User[users.length+1]; 
		// 复制
		System.arraycopy(users, 0, newUsers, 0, users.length);
		// 新增元素
		newUsers[newUsers.length-1] = user;
		
		users = newUsers;
	}
}

3.6.6 Dao
public class UserDao {
	
	public User queryUserByUsername(String username) {
		// 获取所有用户
		User[] users = Database.getUser();
		// 遍历获取指定用户
		for (int i = 0; i < users.length; i++) {
			if (username.equals(users[i].getUsername())) {
				return users[i];
			}
		}
		// 到这里 说明用户名不存在
		return null;
	}
	
	public void add(User user){
		Database.add(user);
	}
}
3.6.7 Service
public class UserService {
	private UserDao userDao = new UserDao();

	
	public User login(User user) throws UserException {
		// 1 用户输入的
		// 2 根据用户查询用户信息
		User oldUser = userDao.queryUserByUsername(user.getUsername());
		// 3 校验
		if (oldUser == null) {
			// 异常源,创建个异常对象
			// throw 会终止程序执行
			throw new UserException("用户名不存在");
		}
		if (!oldUser.getPassword().equals(user.getPassword())) {
			throw new UserException("密码不正确");
		}
		return oldUser;
	}

	
	public void add(User user) throws UserException {
		// 1 根据用户名查询用户信息
		User oldUser = userDao.queryUserByUsername(user.getUsername());
		// 2 判断是否有该用户
		if (oldUser != null) {
			throw new UserException("用户名已存在");
		}
		// 3 添加 
		userDao.add(user);
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5707395.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存