static int div(int a,int b) throws exception{
int c=a/b;
return c;
}
异常的类型
1.编译异常 必须自己处理
2.运行时异常 extends RuntimeException
//异常处理方式: 1.jvm 处理 运行java程序的抽象计算机 异常名称 异常信息 异常出现的位置
// 2.自己处理 (1) try catch finally
// (2) throws
//所有异常的父类都是Exception
写异常
public class test123 {
public static void main(string[] args){
person person=new person();
person.setage(-1);
int age=person.getage();
system.out.println(age);
}
}
新建异常
public class person{
private int age;
public int getage(){
return
}
模拟斗地主2
1.准备一副牌(54)
Map<Integer,String> pohers=new HashMap<>();
String[] colors=new String[]{“黑桃”,”红桃”,”方块”,”梅花”};
String[] number=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+num;
pokers.put(index,pai);
index++;
}
}
pokers.put(index,”小王”);
index++;
pokers.put(index,”大王”);
system.out.println(pokers;)
List pokerIndexs=new ArrayList<>();
for(Integer i:pokers.keySet()){
pokerIndexs.add(i);
}
System.out.println(pokerIndexs);
//2.洗牌(打乱牌序)
Collections.shuffle(pokerIndexs);
System.out.println(pokersIndexs);
//3.留三张底牌
pokerIndexs.remove(index:0);
pokerIndexs.remove(index:0);
pokerIndexs.remove(index:0);
System.out.println(pokerIndexs);
//list 有序 可重复 set 无序 不可以重复
Set lilaoshi=new TreeSet<>();
Set wanglaoshi=new TreeSet<>();
Set malaoshi=new TreeSet<>();
for(int i=0;i<pokerIndexs.size();i++){
int pokerIndexs=pokerIndexs.get(i);
int mod=i%3; //取余数
if(mod0){
lilaoshi.add(pokerIndexs);
}else if(mod1){
wanglaoshi.add(pokerIndexs);
}else{
malaoshi.add(pokerIndexs);
}
}
System.out.println(“lilaoshi: ”+lilaoshi);
System.out.println(“wanglaoshi: ”+wanglaoshi);
System.out.println(“malaoshi: ”+malaoshi);
look(pokers,linxiangtao);
look(pokers,linxiangtao);
look(pokers,linxiangtao);
}
public void look(map<Integer,String> pokers,Set playerpokerIndexs){
List pokerValues=new Arraylist<>();
for(integer index:playerpokerindexs){
pokervalues.add(pokers.get(index));
}
}
}