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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. <span style="font-size:12.0pt;
  2. font-family:宋体;mso-ascii-font-family:Arial;mso-hansi-font-family:Arial;
  3. mso-bidi-font-family:Arial;color:#333333;mso-font-kerning:0pt">下创建一个名为</span><span lang="EN-US" style="font-size:12.0pt;font-family:" arial","sans-serif";mso-fareast-font-family:="" 宋体;color:#333333;mso-font-kerning:0pt"="">Letter</span><span style="font-size:
  4. 12.0pt;font-family:宋体;mso-ascii-font-family:Arial;mso-hansi-font-family:Arial;
  5. mso-bidi-font-family:Arial;color:#333333;mso-font-kerning:0pt">的目录。</span>
复制代码
  1. import java.io.*;
  2. import java.util.Arrays;
  3. import java.util.Scanner;

  4. public class AbsenteeNote {
  5. public static void main(String[] args) throws IOException {
  6.   int select = 0;
  7.   select = welcome();
  8.   if (select == 1 || select == 2) {
  9.    writeLetter(select);
  10.   } else {
  11.    viewLetter();
  12.   }
  13. }

  14. private static int welcome() {
  15.   int num = 0;//用于第一层菜单的选择
  16.   int num1 = 0;//用于第二层菜单的选择
  17.   do {
  18.    System.out.println("1:编写请假条");
  19.    System.out.println("2:查看请假条");
  20.    System.out.print("请选择功能号:");
  21.    Scanner in = new Scanner(System.in);
  22.    num = in.nextInt();
  23.   } while (num != 1 && num != 2);//若输出的数字不为1或2,则继续选择
  24.   if (num == 1) {
  25.    do {
  26.     System.out.println("1:病假");
  27.     System.out.println("2:事假");
  28.     System.out.print("请选择功能号:");
  29.     Scanner in = new Scanner(System.in);
  30.     num1 = in.nextInt();
  31.    } while (num1 != 1 && num1 != 2);
  32.    return num1;
  33.   } else {//当输入的是2(查看请假条),则任意返回一个值。
  34.    return 0;
  35.   }
  36. }

  37. private static void viewLetter() {//查看请假条
  38.   File letter = new File("c:\\Letter\\Letter.txt");
  39.   try {
  40.    FileInputStream view = new FileInputStream(letter);
  41.    byte b[] = new byte[view.available()];
  42.    
  43.    while (view.read(b) != -1) {
  44.     FilterOutputStream viewContain=new FilterOutputStream(System.out);
  45.     System.out.println("假条内容如下:");
  46.     viewContain.write(b);
  47.     viewContain.close();
  48.    }
  49.    view.close();
  50.   } catch (FileNotFoundException e) {
  51.    System.out.println("文件不存在!!!");
  52.    e.printStackTrace();
  53.   } catch (IOException e) {
  54.    e.printStackTrace();
  55.   }

  56. }

  57. private static void writeLetter(int num1) {//写请假条
  58.   String str = new String();
  59.   if (num1 == 1)
  60.    str = "我因发烧,请假一天 ";
  61.   else
  62.    str = "参加婚礼,于4.10请假一天";
  63.   File letter = new File("c:\\Letter\\Letter.txt");
  64.   StringBuilder contain = new StringBuilder();
  65.   Scanner in = new Scanner(System.in);
  66.   System.out.print("寄信人地址:");
  67.   contain.append("寄信人地址:" + in.nextLine() + "\r\n");
  68.   System.out.print("收件人地址:");
  69.   contain.append("收件人地址:" + in.nextLine() + "\r\n");
  70.   System.out.print("签名:");
  71.   contain.append("签名:" + in.nextLine() + "\r\n");
  72.   System.out.print("日期:");
  73.   contain.append("日期:" + in.nextLine() + "\r\n");
  74.   contain.append(str);
  75.   byte[] input = contain.toString().getBytes();
  76.   try {
  77.    FileOutputStream out = new FileOutputStream(letter);
  78.    out.write(input, 0, input.length);
  79.    System.out.println("请假条生成成功!!!");
  80.    out.close();
  81.   } catch (FileNotFoundException e) {
  82.    System.out.println("文件不存在!!!");
  83.    e.printStackTrace();
  84.   } catch (IOException e) {
  85.    e.printStackTrace();
  86.   }
  87. }
  88. }
复制代码


编写一个程序来显示下列选项:
1:编写请假条
2:查看请假条
根据用户的选择执行以下操作。
如果所选的选项是“撰写请假条”,则应显示下列选项。
病假
事假
根据用户的选择,在练习1中所创建的Letter目录下创建一个名为Letter.txt的文件,其内容如下:
寄信人地址 用户输入
收件人地址 用户输入
签名 用户输入
日期 用户输入
正文
病假 我因发烧,请假一天
事假 参见婚礼,于4.10请假一天



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马