黑马初试试题

上传人:飞*** 文档编号:38058516 上传时间:2018-04-26 格式:PDF 页数:14 大小:46.93KB
返回 下载 相关 举报
黑马初试试题_第1页
第1页 / 共14页
黑马初试试题_第2页
第2页 / 共14页
黑马初试试题_第3页
第3页 / 共14页
黑马初试试题_第4页
第4页 / 共14页
黑马初试试题_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《黑马初试试题》由会员分享,可在线阅读,更多相关《黑马初试试题(14页珍藏版)》请在金锄头文库上搜索。

1、package com.itheima; /* * 第 1 题: 有一个字符串 “woaiheimahahaheimaaiwo“ 求该字符串中“heima“ 出现的次数 ; * */ public class Test1 public static void main(String args) / 定义一个字符串“woaiheimahahaheimaaiwo“ String string = “woaiheimahahaheimaaiwo“; / 定义一个字符串“heima“ String string2 = “heima“; / 将目的字符串转成char 类型数组char charArray

2、 = string.toCharArray(); / 将匹配的字符串转成char 类型数组char charArray2 = string2.toCharArray(); / 标志位boolean b = false; / 定义整型变量 ,存放次数int i = 0; / 记录匹配的数组位置int x = 0; / 遍历 char 类型数组for (int j = 0; j set = new TreeSet(Collections.reverseOrder(); while (true) i+; System.out.println(“ 请输入第 “ + i + “个学生信息 ,格式为 (n

3、ame,30,30,30) :“); String message = new Scanner(System.in).nextLine(); / 分割字符串 ,用字符转数组接收String split = message.split(“,“); / 创建 Student 对象Student student = new Student(split0, Double.parseDouble(split1), Double.parseDouble(split2), Double.parseDouble(split3); / 对象加入集合中set.add(student); System.out.pr

4、intln(“ 是否继续 ?(0 结束其他数字继续 :)“); int a = new Scanner(System.in).nextInt(); if (a = 0) System.out.println(“ 录入与结束 !“); break; / 实现文字录入到文件中File file = new File(“stu.txt“); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file); bufferedWriter.write(“ 学生姓名语文成绩数学成绩英语成绩总成绩 “); bufferedWri

5、ter.newLine(); for (Student student : set) bufferedWriter.write(student.getName() + “ “ + student.getChinese() + “ + student.getMath() + “ “ + student.getEnglish() + “ “ + student.getSum(); bufferedWriter.newLine(); bufferedWriter.flush(); / 关流bufferedWriter.close(); / 定义 Student 类class Student impl

6、ements Comparable private String name; private double chinese; private double math; private double english; private double sum; / 空参构造public Student() super(); / TODO Auto-generated constructor stub / 满参构造public Student(String name, double chinese, double math, double english) super(); this.name = n

7、ame; this.chinese = chinese; this.math = math; this.english = english; this.sum = chinese + english + math; / 继承接口重写compareTo 方法Override public int compareTo(Student o) / TODO Auto-generated method stub int i = new Double(this.sum).compareTo(new Double(o.sum); if (i = 0) / 若成绩相同 ,按名字排序return pareTo(

8、o.name); return i; / Treeset 集合要重写hashCode 和 equals 方法Override public int hashCode() return (int) (this.name.hashCode() + this.sum); Override public boolean equals(Object o) if (o instanceof Student) Student a = (Student) o; return this.name.equals(a.name) return false; / 设置和获取方法public String getNam

9、e() return name; public void setName(String name) this.name = name; public double getChinese() return chinese; public void setChinese(double chinese) this.chinese = chinese; public double getMath() return math; public void setMath(double math) this.math = math; public double getEnglish() return engl

10、ish; public void setEnglish(double english) this.english = english; / sum 只有获取方法public double getSum() return sum; package com.itheima; /* * 第 3 题: 请说明 Java中字符 的含义,有什么作用?* 解析 : * 为转义字符*指在其后面跟的字符是具有特殊含义的,它们在字符串中组成了一个用于编译器识别的标 记,*它用于描述一些无法用单个字符描述的特殊符号*如换行符,退格符,制表符等等(他们在window 中分别是 n,b,t ) 。*当编译器读到这个标记

11、的时候,它就知道下一个字符是转义字符,而不是普通的字符* 它会依据转义字符的含义做操作,而不是直接的使用原字符。*/ public class Test3 / 转义字符实现换行public static void main(String args) System.out.print(“an“); System.out.print(“b“); package com.itheima; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; /* *第 4 题: 取出一个字符串中字母出现的次数。如:字符

12、串:“abcdekka27qoq“ ,输出格式为: a(2)b(1)k(2). * */ public class Test4 public static void main(String args) / 定义字符串: “abcdekka27qoq“ String string = “abcdekka27qoq“; / 提取字符char charArray = string.toCharArray(); / 将元素放入map 集合字母为键,个数为值TreeMap map = new TreeMap(); for (char c : charArray) map.put(c, 0); for (

13、char c : charArray) if (map.containsKey(c) int num = map.get(c) + 1; map.remove(c); map.put(c, num); / 迭代集合输出Set entrySet = map.entrySet(); for (Entry entry : entrySet) System.out.print(entry.getKey() + “(“ + entry.getValue() + “)“); package com.itheima; /* * 第 5 题: 有一个类为ClassA,有一个类为ClassB ,在 ClassB

14、中有一个方法b,此方法抛出异常,在 ClassA类中有一个* 方法 a,请在这个方法中调用b,然后抛出异常。 在客户端有一个类为TestC, 有一个方法为c ,请在这个方法中捕* 捉异常的信息。完成这个例子,请说出java 中针对异常的处理机制。* * java 中针对异常的处理机制: * 多异常处理*捕获处理:*1 多个异常分别处理*2 多个异常一次捕获多次处理*3 多个异常一次捕获一次处理*声明抛出异常:*声明上使用 ,一次声明多个异常*运行时异常被抛出可以不处理。即不捕获也不声明抛出。*如果父类抛出了多个异常,子类覆盖父类方法时,只能抛出相同的异常或者是他的子集*父类方法没有抛出异常,子

15、类覆盖父类该方法时也不可抛出异常。此时子类产生该异常,只能捕获处理,不能声明抛出*当多异常处理时,捕获处理,前边的类不能是后边类的父类* */ public class Test5 public static void main(String args) / 创建 TestC对象 ,并调用方法TestC cilent = new TestC(); cilent.c(); System.out.println(“ 结束 “); class A public static void a() throws Exception / 调用 B的方法抛出异常 B.b(); class B public static void b() / 定义方法抛出异常try throw new Exception(“ throw by ClassB.methodB “); catch (Exception e) e.printStackTrace(); class TestC / 捕获异常void c() try A.a(); catch (Exception e) e.printStackTrace(); package com.itheima; /* * 第 6 题:

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 研究报告 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号