斗地主之洗牌发牌排序 java常见异常及处理

培训第5天打卡,斗地主之洗牌发牌排序java基础集合案例
package test;

import zhongruan.service.Lady;

import java.util.*;

public class Test0809 {
public static void main(String[] args) {
Map<Integer,String> pokers=new HashMap<>();
String[] colors=new String[]{“红心”,“方块”,“黑桃”,“草花”};
String[] numbers=new String[]{“A”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”};
int index=1;
for (String color:colors) {
for (String number : numbers) {
String pai = color + number;
pokers.put(index, pai);
index++;
}
}
pokers.put(index,“小王”);
index++;
pokers.put(index,“大王”);
System.out.println(pokers);
List pokerIndex=new ArrayList<>();
for(Integer i:pokers.keySet()){
pokerIndex.add(i);
}
System.out.println(pokerIndex);
Collections.shuffle(pokerIndex);
System.out.println(pokerIndex);
pokerIndex.remove(0);
pokerIndex.remove(0);
pokerIndex.remove(0);
System.out.println(pokerIndex);
Set zhangsan=new TreeSet<>();
Set lisi=new TreeSet<>();
Set wangwu=new TreeSet<>();
for (int i=0;i<pokerIndex.size();i++){
int pokerindex=pokerIndex.get(i);
int mod=i%3;
if(mod0){
zhangsan.add(pokerindex);
}else if(mod
1){
lisi.add(pokerindex);
}else{
wangwu.add(pokerindex);
}
}
System.out.println(“zhangsan:”+zhangsan);
System.out.println(“lisi:”+lisi);
System.out.println(“wangwu:”+wangwu);
look(pokers,zhangsan);
look(pokers,lisi);
look(pokers,wangwu);
}
public static void look(Map<Integer,String> pokers,Set pokerPokerIndex){
List pokerValues=new ArrayList<>();
for(Integer index:pokerPokerIndex){
pokerValues.add(pokers.get(index));
}
System.out.println(pokerValues);
}
}
这里普及一个知识点:
list:无序不可重复
set:有序可重复(排序主要利用了set的有序性)

接下来我们学习了java常见异常及处理方式

1、 在某一路径下执行编译好的class文件出错。
异常如下:

E:\liwy>java Test98
Exception in thread “main” java.lang.NoClassDefFoundError: Test98

如果出现了以上错误提示,常见问题有如下两种:1 大小写写错了, 2 路径不正确。

2、数组错误,访问不存在的数组,数组超出绑定,代码如下:

public class ArrayDemo
{
public static void main(String args[]){
int array[] = null; //声明数组
array = new int[3]; //为数组开辟空间,大小为3
for(int i=0;i<array.length;i++){
System.out.println(“array[”+i+"]="+i);
}

//访问数组没有开辟的下标,这时会报异常

System.out.println(“array[3]=”+array[3]);
}
}
异常如下:

array[0]=0
array[1]=1
array[2]=2

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3
at ArrayDemo.main(ArrayDemo.java:11)

以上就是数组的索引超出绑定,就是表示数组越界。

3、某个类没有实例化,访问类属性时,出现空指针异常

class Person{
String name ; // 声明姓名属性
int age ; // 声明年龄属性
public void tell(){ // 取得信息
System.out.println(“姓名:” + name + “,年龄:” + age) ;
}
};

public class ClassDemo03{
public static void main(String args[]){
Person per = null ; // 声明对象
//per = new Person() ; // 实例化对象
per.name = “张三” ; // 为姓名赋值
per.age = 30 ; // 为年龄赋值
per.tell() ; // 调用方法,打印信息
}
};

异常如下:
Exception in thread “main” java.lang.NullPointerException
at ClassDemo03.main(ClassDemo03.java:12)

4、错误的多态,出现异常

class A{ // 定义类A
public void fun1(){ // 定义fun1()方法
System.out.println(“A --> public void fun1(){}”) ;
}
public void fun2(){
this.fun1() ; // 调用fun1()方法
}
};

class B extends A{
public void fun1(){ // 此方法被子类覆写了
System.out.println(“B --> public void fun1(){}”) ;
}

public void fun3(){
System.out.println(“B --> public void fun3(){}”) ;
}
};

public class PolDemo03{
public static void main(String asrgs[]){
A a = new A() ; // 实例化了一个父类对象
B b = (B)a ; // 发生了向下转型关系
b.fun1() ;
b.fun2() ;
b.fun3() ;
}
};

异常如下:
Exception in thread “main” java.lang.ClassCastException: A
at PolDemo03.main(PolDemo03.java:20)

5、两个数字相除,被除数为0,出现异常
public class ExceptionDemo01{
public static void main(String args[]){
System.out.println("********** 计算开始 *") ;
int i = 10 ; // 定义整型变量
int j = 0 ; // 定义整型变量
int temp = i / j ; // 此处产生了异常
System.out.println(“两个数字相除的结果:” + temp) ;
System.out.println("
计算结束 ***********") ;
}
};

异常如下:
********** 计算开始 ***********

Exception in thread “main” java.lang.ArithmeticException: / by zero
at ExceptionDemo01.main(ExceptionDemo01.java:6)

6、两个数字相除,输入两个参数,分别为字母a 和b,出现异常,数字格式化出问题

public class ExceptionDemo04{
public static void www.gzlij.com main(String args[]){
System.out.println("********** 计算开始 *") ;
int i = 0 ; // 定义整型变量
int j = 0 ; // 定义整型变量
try{
String str1 = args[0] ; // 接收第一个参数
String str2 = args[1] ; // 接收第二个参数
i = Integer.parseInt(str1) ; // 将第一个参数由字符串变为整型
j = Integer.parseInt(str2) ; // 将第二个参数由字符串变为整型
int temp = i / j ; // 此处产生了异常
System.out.println(“两个数字相除的结果:” + temp) ;
System.out.println("----------------------------") ;
}catch(ArithmeticException e){ // 捕获算术异常
System.out.println(“出现异常了:” + e) ;
}
System.out.println("
计算结束 ***********") ;
}
};

异常如下:

********** 计算开始 ***********

Exception in thread “main” java.lang.NumberFormatException: For input string: “a”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at ExceptionDemo04.main(ExceptionDemo04.jav

7、两个数字相除,输入两个参数,如果两个参数什么也不输入,出现异常,数组超出绑定。

异常如下:

********** 计算开始 ***********
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
at ExceptionDemo04.main(ExceptionDemo04.java:7)

7、assert断言的使用,当断言结果不对出现异常。

public class Test
{
public static void main(String args[]){
int i[] = {1,2,3}; // 数组长度为3
assert i.length==0; // 此处断言数组长度为0
}
}

异常如下:

D:\d代码>java -ea Test
Exception in thread “main” java.lang.AssertionError
at Test.main(Test.java:5)

断言需要在运行时需要加上“-ea”,如上java –ea 类名。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值