JAVA--U2 异常类处理及常用类复习

目录

实习一   杭州亚运会奖牌榜!!!【China】

题目描述

 题解

实习二 实用类Calendar应用

题目描述

题解

实习三 异常类的处理--银行存款存&取 

题目描述

题解

 实习四  正则表达式应用!

 题目描述

题解


实习一   杭州亚运会奖牌榜!!!【China】

--From 某乒乓球迷
题目描述

什么实力不用多说!!!hhhhhhhh

 题解
//Country类

public class Country implements Comparable{
	String name;
	int Golde;
	int Silver;
	int Bronze;
	public Country(String name,int g,int s,int b) {
		// TODO Auto-generated constructor stub
        this.name = name;
		this.Golde = g;
        this.Silver = s;
		this.Bronze = b;
	}
	//比较方式就是金银铜 国家名字顺序比较
	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		Country temp  = (Country)o;
		if(this.Golde==temp.Golde) {
			if(this.Silver==temp.Silver) {
				if(this.Bronze==temp.Bronze) {
					return this.name.compareTo(temp.name); 
				}else {
					return temp.Bronze-this.Bronze;							
				}
			}else {
				return temp.Silver-this.Silver;			
			}
		}else {
			return temp.Golde-this.Golde;
		}
	}
    //重写toString()
	public String toString() {
		return "国家:"+name+" "+this.Golde+" "+this.Silver+" "+this.Bronze;
	}
}
//Test类
//针对测试过程中的大量数据 建议提前放置在txt中 便于直接cv


public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		Country country[] = new Country[20];
		System.out.println("请输入各个国家获奖信息:");
		for(int i = 0;i<20;i++) {
			String name = scanner.next();
			int g  = scanner.nextInt();
			int s  = scanner.nextInt();
			int b  = scanner.nextInt();
			country[i] = new Country(name, g,s,b);
		}
		System.out.println("\n排序前:");
		for(int i=0;i<20;i++) {
		    System.out.println(country[i]);	
		}
        //系统自带的排序 注意与排序器区分
		Arrays.sort(country);
		System.out.println("排序后:");
		for(int i=0;i<20;i++) {
		    System.out.println(country[i]);	
		}
	}
}

实习二 实用类Calendar应用

题目描述

编写一个能够显示日历的程序并实现以下要求:

题解

本日历仅实现在控制台输出的功能,结合页面设计的日历在U4中有具体的设计过程;

//Calendarr类 - 自设计

//根据不同的年月构造的日历
public class Calendarr {
	int year;
	int month;
	int day;
	public Calendarr(int y,int m) {
		// TODO Auto-generated constructor stub
		this.year = y;
		this.month = m;   
 if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
			day = 31;
		}else if(month==4||month==6||month==9||month==11) {
			day =30;
		}else {
			if(y%4==0&&y%100!=0||y%400==0) {
				day= 29;
			}else {
				day = 28;
			}
		}
	}
	public String[] getCalendar() {
		String data[] = new String[42];
		//实例化日历--不用new 
		Calendar calendar = Calendar.getInstance();
		//set 月份时 一律 - 1
	    //get 月份时 一律 + 1
		calendar.set(year, month-1, 1);
		//确定本月第一天是周几 周日到周六 返回1-7
		int first = calendar.get(Calendar.DAY_OF_WEEK)-1;
		for(int i =0;i<first;i++) {
			data[i] = "";
		}
		for(int i= first;i<first+day;i++) {
			data[i] = (i-first+1)+"";
		}
		for(int i = first+day;i<data.length;i++) {
			data[i] = "" ;		
		}
		return data;
	}
}
//Test测试

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		
        // LocalDate类获取当前时间
		LocalDate date = LocalDate.now();
		// 当前日期的年份和月份
		int year = date.getYear();
		int month = date.getMonthValue();

		Calendarr c = new Calendarr(year, month);
		String data[] = c.getCalendar();
		
		String ss = "日一二三四五六";
		char ssArray[] = ss.toCharArray();
		System.out.println(year+"年"+month+"月");
		System.out.print(" ");
		for (int i = 0; i < ssArray.length; i++) {
			System.out.printf("%2c", ssArray[i]);
		}
		for (int i = 0; i < data.length; i++) {
			if (i % 7 == 0) {
				System.out.println("\n");
			}
			System.out.printf("%3s", data[i]);
		}
        //循环实现不同月的日历切换
		while (true) {
			System.out.println("\n\n请输入(p)显示上一月日历,输入(n)显示下一月日历");
			String str = scanner.next();
			if (str.equals("p")) {
				month--;
				if (month == 0) {
					month = 12;
					year--;
				}
			} else if (str.equals("n")) {
				month++;
				if (month == 13) {
					month = 0;
					year++;
				}
			}
			Calendarr c1 = new Calendarr(year, month);
			String data1[] = c1.getCalendar();
			System.out.println(year+"年"+month+"月");
			System.out.print(" ");
			for (int i = 0; i < ssArray.length; i++) {
				System.out.printf("%2c", ssArray[i]);
			}
			for (int i = 0; i < data1.length; i++) {
				if (i % 7 == 0) {
					System.out.println("\n");
				}
				System.out.printf("%3s", data1[i]);
			}
		}
	}

实习三 异常类的处理--银行存款存&取 

题目描述

题解
//BankAccount银行账户

public class BankAccount {
	int amount;//余额
	public BankAccount(int amount) {
		// TODO Auto-generated constructor stub
		this.amount = amount;
	}
	//存钱--抛出负数
	void deposite(int dAmount) throws NegativeException{
		if(dAmount<0) {
			throw new NegativeException("save",dAmount);
		}
		amount+=dAmount;
	}
	//取钱--抛出负数异常&余额不足
	void withdraw(int dAmount) throws NegativeException,InsufficientException{
		if(dAmount<0) {
			throw new NegativeException("withdraw",dAmount);
		}
		if(amount<dAmount) {
			throw new InsufficientException();
		}
		amount-=dAmount;
	}
	//显示当前账户
	void showBalance() {
		System.out.println("当前余额:"+amount);
	}
}
//余额不足的情况--异常类1
public class InsufficientException extends Exception{
	String mess;
	public InsufficientException() {
		// TODO Auto-generated constructor stub
		mess = "余额不足";
	}
	public String warnMess() {
		return mess;
	}
}


//存钱取钱负数情况--异常类2
public class NegativeException extends Exception{
	String mess = null;
	public NegativeException(String s,int money) {
		// TODO Auto-generated constructor stub
		if(s.equals("save")) {
			mess = "存入金额非法:"+money;
		}else if(s.equals("withdraw")){
			mess = "取出金额非法:"+money;	
		}
	}
	public String warnMess() {
		return mess;
	}
}

//Test测试

public class ExceptionTest {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//初始化一个2000余额的账户
		BankAccount bank = new BankAccount(2000); 
		bank.showBalance();
		Scanner scanner = new Scanner(System.in);
		int money=0;
		String operation = null;
		while(true) {
			System.out.println("输入操作及金额:");
			operation = scanner.next();
			money = scanner.nextInt();
			if(operation.equals("save")) {
				//执行存钱
				try {
					bank.deposite(money);
					bank.showBalance();
								
				} catch (NegativeException e) {
					// TODO Auto-generated catch block
					System.out.println(e.warnMess());
//					e.printStackTrace();
				}
			}else if(operation.equals("withdraw")) {
				try {
					bank.withdraw(money);
					bank.showBalance();
					
				} catch (NegativeException e) {
					// TODO Auto-generated catch block
					System.out.println(e.warnMess());
//	     			e.printStackTrace();
				} catch (InsufficientException e) {
					// TODO Auto-generated catch block
					System.out.println(e.warnMess());
//					e.printStackTrace();
				}
			}	
		}
	}
}

 实习四  正则表达式应用!

 题目描述

题解
//Person类设计略

//Test类

public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		String str[] = new String[8];
		for(int i =0;i<8;i++) {
			str[i] = scanner.next();		
		}
		Person person1= new Person("令狐冲","13754897852");
		Person person2= new Person("乔峰","18412345612");
		Person person3= new Person("虚竹","15828737890");
		Person person4= new Person("任我行","13754897852");
		
		String s = null;
		for(int i=0;i<8;i++) {
			s += str[i];
		}
		//匹配的字符串
	    String regex_num =  "1[3|4|5|8][0-9]\\d{8}";
		String regex_name = "[\\u4E00-\\u9FA5]+";
		//借助Pattern和Matcher匹配
		Pattern p_num = Pattern.compile(regex_num);   //模式对象
		Matcher m_num = p_num.matcher(s);             //匹配对象
		while(m_num.find()) {
			String item1 = m_num.group();
			System.out.println(item1);
		}
		
		Pattern p_name = Pattern.compile(regex_name);
		Matcher m_name = p_name.matcher(s);
		while(m_name.find()) {
			String item2 = m_name.group();
			System.out.println(item2);
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值