目录
Java练习第七章
6-71 jmu-Java-06异常-多种类型异常的捕获
6-72 设计一个简单通信类
6-73 自定义异常类,判断是否构成三角形
7-71 jmu-Java-06异常-02-使用异常机制处理异常输入
Java练习第八章
6-81 tjrac - Java集合类之List的ArrayList之增删改查
6-82 ArrayLis编程实现求最大最小值
6-83 HashSet-存放用户账户和密码
6-84 TreeSet排序-学生信息按分数排序放入集合
6-85 tjrac - Java集合类之Map的HashMap之常用方法的使用
6-86 使用HashMap编程输入诗的名称查询出诗的内容
catch (NumberFormatException e) {
System.out.println("number format exception");
System.out.println("java.lang.NumberFormatException");
}
catch (IllegalArgumentException e) {
System.out.println("illegal argument exception");
System.out.println("java.lang.IllegalArgumentException");
}
catch (Exception e) {
System.out.println("other exception");
System.out.println("java.lang.Exception");
}
import java.util.Scanner;
class Tel {
private String name;
private String mobilephone;
public Tel(String name, String mobilephone) throws Exception {
if(!mobilephone.matches("\\d{11}")) {
throw new Exception("手机号码必须是数字,且为11位");
}
else {
this.name = name;
this.mobilephone = mobilephone;
}
}
public String getName() {
return name;
}
public String getMobilephone() {
return mobilephone;
}
}
import java.util.Scanner;
class NotSanjiaoException extends Exception {
public NotSanjiaoException() {
super("三边长不能构成三角形");
}
}
class Sanj {
double x,y,z;
double area;
public Sanj(double x, double y, double z) throws NotSanjiaoException {
this.x = x;
this.y = y;
this.z = z;
if(!(x+y>z && y+z>x && x+z>y)) {
throw new NotSanjiaoException();
}
}
public double getArea() {
double s = (x+y+z)/2;
return Math.sqrt(s*(s-x)*(s-y)*(s-z));
}
}
import java.util.Arrays;
import java.util.Scanner;
class NumberFormatException extends RuntimeException {
public NumberFormatException() {
}
public NumberFormatException(String message) {
System.out.println("java.lang.NumberFormatException: For input string: \""+message+"\"");
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] arr = new int[n];
int i = 0;
while(i < n) {
String tmp = scan.next();
try {
if(!tmp.matches("\\d")) {
throw new NumberFormatException(tmp);
}
else {
arr[i] = Integer.parseInt(tmp);
i++;
}
}catch (NumberFormatException e) {
e.printStackTrace();
}
}
System.out.println(Arrays.toString(arr));
}
}
// 第一步:接收给定的一行字符串
Scanner scan = new Scanner(System.in);
String into = scan.nextLine();
// 第二步:切割字符串
String[] get = into.split(" ");
// 第三步:创建集合
ArrayList arrayList = new ArrayList<>();
// 第四步:往集合中添加元素
for (int i = 0; i < get.length; i++) {
arrayList.add(get[i]);
}
// 第五步:删除第一个元素和最后一个元素
arrayList.remove(0);
arrayList.remove(arrayList.size()-1);
// 第六步:往集合中添加hello和educoder
arrayList.add("hello");
arrayList.add("educoder");
// 第七步:修改集合中第三个元素为list
arrayList.set(2,"list");
// 第八步:打印集合
System.out.println(arrayList.toString());
for (int i = 0; i < n; i++) {
list.add(i,scan.nextInt());
}
int max = list.get(0);
int min = list.get(0);
for (int i = 0; i < list.size(); i++) {
int tmp = list.get(i);
if(max < tmp) {
max = tmp;
}
if(min > tmp) {
min = tmp;
}
}
System.out.println("数列是:"+list.toString());
class User {
String name;
String id;
public User(String name,String id) {
this.name = name;
this.id = id;
}
@Override
public String toString() {
return "{ 用户名:"+name+" 密码:"+id+"}";
}
@Override
public int hashCode() {
return this.name.hashCode()+this.id.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if(!(obj instanceof User))
return false;
User u = (User)obj;
if(this.name.equals(u.name)&&this.id.equals(u.id))
return true;
else
return false;
}
}
class Student implements Comparable{
String number;
String name;
int mark;
public Student(String number,String name,int mark) {
this.mark = mark;
this.name = name;
this.number = number;
}
@Override
public String toString() {
return "学号: " + number + " 姓名: " + name + " 成绩: " + mark;
}
@Override
public int compareTo(Student arg0) {
// TODO Auto-generated method stub
if(this.mark>arg0.mark)
return 1;
else if(this.mark==arg0.mark)
return 0;
else
return -1;
}
}
/********** Begin **********/
// 第一步:接收给定字符串
Scanner scan = new Scanner(System.in);
String into = scan.next();
// 第二步:切割字符串
String[] cut = into.split(",|:");
// 第三步:创建 HashMap 集合,key为菜名,value为价格
HashMap map = new HashMap<>();
// 第四步:添加数据到集合中
for (int i = 0; i < cut.length; i+=2) {
map.put(cut[i],cut[i+1]);
}
// 第五步:往集合中添加给定的一道菜
map.put("lamb","50");
// 第六步:输出所有菜名
Set keys = map.keySet();
//Iterator iter = keys.iterator();
System.out.println(keys);
// 第七步:删除红烧鱼和小炒牛肉这两道菜后输出集合长度
map.remove("红烧鱼");
map.remove("小炒牛肉");
System.out.println(map.size());
// 第八步:打印集合
System.out.println(map.toString());
/********** End **********/
String poem = scan.next();
mp.put("春晓","春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。");
mp.put("静夜思","床前明月光,疑是地上霜。举头望明月,低头思故乡。");
mp.put("咏鹅","鹅鹅鹅,曲项向天歌。白毛浮绿水,红掌拨清波。");
System.out.println(mp.get(poem));
Set key = mp.keySet();
Collection value = mp.values();
Iterator i1 = value.iterator();
Iterator i2 = key.iterator();
while(i1.hasNext()) {
String v = i1.next();
String k = i2.next();
System.out.println(k+"---"+v);
}