本文主要是介绍jmu-PTA Java答案汇总(下),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
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编程输入诗的名称查询出诗的内容
Java练习第七章
6-71 jmu-Java-06异常-多种类型异常的捕获
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");}
6-72 设计一个简单通信类
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;}}
6-73 自定义异常类,判断是否构成三角形
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));}
}
7-71 jmu-Java-06异常-02-使用异常机制处理异常输入
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));}
}
Java练习第八章
6-81 tjrac - Java集合类之List的ArrayList之增删改查
// 第一步:接收给定的一行字符串Scanner scan = new Scanner(System.in);String into = scan.nextLine();// 第二步:切割字符串String[] get = into.split(" ");// 第三步:创建集合ArrayList<String> arrayList = new ArrayList<>();// 第四步:往集合中添加元素for (int i = 0; i < get.length; i++) {arrayList.add(get[i]);}// 第五步:删除第一个元素和最后一个元素arrayList.remove(0);arrayList.remove(arrayList.size()-1);// 第六步:往集合中添加hello和educoderarrayList.add("hello");arrayList.add("educoder");// 第七步:修改集合中第三个元素为listarrayList.set(2,"list");// 第八步:打印集合System.out.println(arrayList.toString());
6-82 ArrayLis编程实现求最大最小值
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());
6-83 HashSet-存放用户账户和密码
class User {String name;String id;public User(String name,String id) {this.name = name;this.id = id;}@Overridepublic String toString() {return "{ 用户名:"+name+" 密码:"+id+"}";}@Overridepublic int hashCode() {return this.name.hashCode()+this.id.hashCode();}@Overridepublic 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;elsereturn false;}
}
6-84 TreeSet排序-学生信息按分数排序放入集合
class Student implements Comparable<Student>{String number;String name;int mark;public Student(String number,String name,int mark) {this.mark = mark;this.name = name;this.number = number;}@Overridepublic String toString() {return "学号: " + number + " 姓名: " + name + " 成绩: " + mark;}@Overridepublic int compareTo(Student arg0) {// TODO Auto-generated method stubif(this.mark>arg0.mark)return 1;else if(this.mark==arg0.mark)return 0;elsereturn -1;}}
6-85 tjrac - Java集合类之Map的HashMap之常用方法的使用
/********** Begin **********/// 第一步:接收给定字符串Scanner scan = new Scanner(System.in);String into = scan.next();// 第二步:切割字符串String[] cut = into.split(",|:");// 第三步:创建 HashMap 集合,key为菜名,value为价格HashMap<String,String> map = new HashMap<>();// 第四步:添加数据到集合中for (int i = 0; i < cut.length; i+=2) {map.put(cut[i],cut[i+1]);}// 第五步:往集合中添加给定的一道菜map.put("lamb","50");// 第六步:输出所有菜名Set<String> keys = map.keySet();//Iterator<String> iter = keys.iterator();System.out.println(keys);// 第七步:删除红烧鱼和小炒牛肉这两道菜后输出集合长度map.remove("红烧鱼");map.remove("小炒牛肉");System.out.println(map.size());// 第八步:打印集合System.out.println(map.toString());/********** End **********/
6-86 使用HashMap编程输入诗的名称查询出诗的内容
String poem = scan.next();mp.put("春晓","春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。");mp.put("静夜思","床前明月光,疑是地上霜。举头望明月,低头思故乡。");mp.put("咏鹅","鹅鹅鹅,曲项向天歌。白毛浮绿水,红掌拨清波。");System.out.println(mp.get(poem));Set<String> key = mp.keySet();Collection<String> value = mp.values();Iterator<String> i1 = value.iterator();Iterator<String> i2 = key.iterator();while(i1.hasNext()) {String v = i1.next();String k = i2.next();System.out.println(k+"---"+v);}
这篇关于jmu-PTA Java答案汇总(下)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!