A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小丑 高级黑马   /  2015-7-9 15:59  /  845 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. <span style="line-height: 2.2em;">package me.zhouyou.io;</span>
复制代码
然而并没有什么用。。纯属发上来得点币。。{:3_64:}
names.zip (594 Bytes, 下载次数: 204)

3 个回复

倒序浏览
  1. package me.zhouyou.io;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Random;
  9. import java.util.Scanner;

  10. public class GetRandomName {

  11.         private static BufferedReader br = null;
  12.         private static ArrayList<String> al = null;
  13.         private static Scanner sc = new Scanner(System.in);
  14.         private static Scanner scanner = new Scanner(System.in);

  15.         public static void main(String[] args) throws IOException {

  16.                 System.out.println("**********  欢迎来到随机点名器~  **********");

  17.                 // 封装数据源
  18.                 br = new BufferedReader(new FileReader("names.txt"));
  19.                 // 将名字添加到集合
  20.                 al = new ArrayList<String>();
  21.                 String line = null;
  22.                 while ((line = br.readLine()) != null) {
  23.                         al.add(line);
  24.                 }

  25.                 while (true) {
  26.                         System.out.println();
  27.                         System.out.println("*******   请输入数字选择 *******");
  28.                         System.out.println("* 1,随机点名   ");
  29.                         System.out.println("* 2,添加名字   ");
  30.                         System.out.println("* 3,删除名字   ");
  31.                         System.out.println("* 4,名字列表   ");
  32.                         System.out.println("* 5,关闭   ");

  33.                         // 获取用户输入
  34.                         int number = sc.nextInt();

  35.                         switch (number) {
  36.                         case 1:
  37.                                 // 获取随机名
  38.                                 getRandomName();
  39.                                 break;
  40.                         case 2:
  41.                                 // 添加名字
  42.                                 getName();
  43.                                 break;
  44.                         case 3:
  45.                                 // 删除名字
  46.                                 printArraylist();
  47.                                 removeAL();
  48.                                 break;
  49.                         case 4:
  50.                                 // 输出集合
  51.                                 printArraylist();
  52.                                 break;
  53.                         case 5:
  54.                                 // 关闭
  55.                                 br.close();
  56.                                 sc.close();
  57.                                 scanner.close();
  58.                                 System.exit(0);
  59.                                 break;
  60.                         default:
  61.                                 System.out.println("输入有误,请输入(1-5)内的数字,如: 1 ");
  62.                                 break;
  63.                         }
  64.                 }

  65.         }

  66.         // 点名方法
  67.         public static void getRandomName() throws IOException {
  68.                 // 获取随机数
  69.                 Random r = new Random();
  70.                 int index = r.nextInt(al.size());

  71.                 System.out.println("随机点名为:" + al.get(index));

  72.         }

  73.         // 添加名字
  74.         private static void getName() throws IOException {
  75.                 System.out.println("请输入一个要添加的名字:");
  76.                 String name = scanner.nextLine();
  77.                 if (al.contains(name)) {
  78.                         System.out.println(name + ",已经存在!");

  79.                 } else {
  80.                         al.add(name);
  81.                         System.out.println(name + ",成功添加!");
  82.                         // 将集合中的数据重新写入到文件中
  83.                         BufferedWriter bw = new BufferedWriter(new FileWriter("names.txt"));
  84.                         for (String s : al) {
  85.                                 bw.write(s);
  86.                                 bw.newLine();
  87.                                 bw.flush();
  88.                         }
  89.                         bw.close();
  90.                 }
  91.         }

  92.         // 删除方法
  93.         private static void removeAL() throws IOException {
  94.                 System.out.println("请输入需要删除名字的编号:");
  95.                 int index = sc.nextInt();
  96.                 if (index > 0 && index < al.size()) {
  97.                         System.out.println(al.get(index)+",成功删除!");
  98.                         al.remove(index);
  99.                         // 将集合中的数据重新写入到文件中
  100.                         BufferedWriter bw = new BufferedWriter(new FileWriter("names.txt"));
  101.                         for (String s : al) {
  102.                                 bw.write(s);
  103.                                 bw.newLine();
  104.                                 bw.flush();
  105.                         }
  106.                         bw.close();
  107.                 } else {
  108.                         System.out.println("输入有误:输入的编号没有对应的名字");
  109.                 }
  110.         }

  111.         // 遍历集合
  112.         private static void printArraylist() {
  113.                 System.out.println("共有如下名字:");

  114.                 for (int x = 1; x < al.size(); x++) {
  115.                         System.out.println(x + ":" + "\t" + al.get(x));
  116.                 }
  117.         }

  118. }
复制代码


回复 使用道具 举报
币没有,我是来消灭零回复的!哈哈
回复 使用道具 举报
bingo54 发表于 2015-7-9 16:14
币没有,我是来消灭零回复的!哈哈

感谢。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马