zoukankan      html  css  js  c++  java
  • 【0809试题】

     题目一:

    从控制台录入一串英文字符,如:wqnmlgb,ndy,dsb,,,请用代码把这些和谐掉,并用*代替。

    和谐敏感词

     ————————————————————————————————————

    题目二:

    给你一组字符串如:iu7i8hy4jnb2,让你编程输出里面的数字:7842

     1 package ceshi;
     2 
     3 import java.util.Scanner;
     4 
     5 /**
     6  * 给你一组字符串如:iu7i8hy4jnb2,让你编程输出里面的数字:7842
     7  * 
     8  * @author L
     9  *
    10  */
    11 public class Outnum {
    12     static Scanner sc = new Scanner(System.in);
    13 
    14     public static void main(String[] args) {
    15         System.out.println("随机输入内容:");
    16         String content = sc.next();
    17         String content2 = "";
    18         if (content != null && !"".equals(content)) {
    19             for (int i = 0; i < content.length(); i++) {
    20                 // 数字对应的ASCII码的范围是48-57
    21                 if (content.charAt(i) >= 48 && content.charAt(i) <= 57) {
    22                     content2 += content.charAt(i);
    23                 }
    24             }
    25         }
    26         System.out.println(content2);
    27     }
    28 }
    输入字符串,只输出数字

    ——————————————————————————————————————————

    题目三:

    实现简易字符串压缩算法:一个长度最大为128的字符串,由字母a-z或者A-Z组成,将其中连续出现2次以上(含2次)的字母转换为字母和出现次数,

    * 以达到压缩目的。

    * 输入:AAAABBBB

    * 输出:A4B4

    * 输入:"AAAABBBBCCCCccccddddeeaxckoiiiii"

    * 输出:A4B4C4c4d4e2axckoi5

     1 package ceshi;
     2 
     3 import java.util.Scanner;
     4 
     5 /**
     6  * 实现简易字符串压缩算法:一个长度最大为128的字符串, 由字母a-z或者A-Z组成,将其中连续出现2次以上(含2次)的字母转换为字母和出现次数,
     7  * 
     8  * @author L
     9  *
    10  */
    11 public class Repeat {
    12     static Scanner sc = new Scanner(System.in);
    13 
    14     public static void main(String[] args) {
    15         System.out.println("随机输入内容:");
    16         String content = sc.next();
    17         char[] arr = content.toCharArray();
    18         StringBuilder sb = new StringBuilder();
    19         for (int i = 0; i < arr.length;) {
    20             sb.append(arr[i]);
    21             int j = i;
    22             while (j < arr.length && arr[i] == arr[j]) {
    23                 j++;
    24             }
    25             if (j - i > 1) {
    26                 sb.append(j - i);
    27             }
    28             i = j;
    29         }
    30         System.out.println(sb.toString());
    31 
    32     }
    33 
    34 }
    字符串压缩

    ————————————————————————————————————

    题目四

     创建一个文件,名称为javabigdata,在此文件夹下创建一个a.txt的文件,并写入如下内容:I love china,I love beiijng,the Beijing is the capatial of China.,然后将a.txt复制到此文件夹下为b.txt

     1 package ceshi;
     2 
     3 import java.io.*;
     4 
     5 /**
     6  * 磁盘创建文件,写入文件,复制该文件
     7  * 
     8  * @author L
     9  *
    10  */
    11 public class Copy {
    12     public static void main(String[] args) {
    13         File f = new File("D:\test");
    14 
    15         // 创建文件
    16         if (!f.exists()) {
    17             f.mkdir();
    18             System.out.println("文件夹创建成功!");
    19         } else {
    20             System.out.println("文件夹已存在!");
    21         }
    22 
    23         // 文件写入
    24         FileOutputStream fos = null;
    25         String str = "I love china,I love beiijng,the Beijing is the capatial of China.";
    26         byte[] words = str.getBytes();
    27         try {
    28             fos = new FileOutputStream("D:\test\a.txt");
    29             fos.write(words);
    30             System.out.println("内容写入完成!");
    31         } catch (FileNotFoundException e) {
    32             // TODO Auto-generated catch block
    33             e.printStackTrace();
    34         } catch (IOException e) {
    35             // TODO Auto-generated catch block
    36             e.printStackTrace();
    37         }
    38 
    39         try {
    40             fos.close();
    41         } catch (IOException e1) {
    42             // TODO Auto-generated catch block
    43             e1.printStackTrace();
    44         }
    45         FileInputStream fis = null;
    46         try {
    47             fis = new FileInputStream("D:\test\a.txt");
    48             fos = new FileOutputStream("D:\test\b.txt");
    49             fos.write(words);
    50         } catch (FileNotFoundException e) {
    51             // TODO Auto-generated catch block
    52             e.printStackTrace();
    53         } catch (IOException e) {
    54             // TODO Auto-generated catch block
    55             e.printStackTrace();
    56         }
    57 
    58         try {
    59             fos.close();
    60             fis.close();
    61             System.out.println("复制完成!");
    62         } catch (IOException e) {
    63             // TODO Auto-generated catch block
    64             e.printStackTrace();
    65         }
    66 
    67     }
    68 }
    创建写入并复制

    ——————————————————————————————————————-

    题目五

    模拟一个在控制台不断按时分秒打印的电子表

     1 package ceshi;
     2 
     3 /**
     4  * 模拟一个在控制台不断按时分秒打印的电子表
     5  * @author L
     6  */
     7 import java.text.SimpleDateFormat;
     8 import java.util.Date;
     9 
    10 public class Time {
    11     public static void main(String[] args) {
    12         // Thread thread=new Thread();
    13         SimpleDateFormat sdf = new SimpleDateFormat("北京时间:HH:mm:ss");
    14         while (true) {
    15             Date date = new Date();
    16             System.out.println(sdf.format(date));
    17             try {
    18                 Thread.sleep(1000);
    19             } catch (InterruptedException e) {
    20                 // TODO Auto-generated catch block
    21                 e.printStackTrace();
    22             }
    23         }
    24     }
    25 }
    北京时间

    ————————————————————————————————————

    题目六

    创建两个线程模拟两个人【一个实现runnable接口,一个继承thread类】,分别叫jack和rose,模拟两人在对话1000次

     1 package ceshi.say;
     2 
     3 public class Rose implements Runnable{
     4     private int num=500;
     5     public void Rose() {
     6         this.num=num;
     7     }
     8     
     9     @Override
    10     public void run() {
    11         // TODO Auto-generated method stub
    12         for(int i=num-1;i>=0;i--) {
    13             Thread t=Thread.currentThread();
    14             System.out.println("rose说:你是谁呀?");
    15             
    16             try {
    17                 Thread.sleep(1000);
    18             } catch (InterruptedException e) {
    19                 // TODO Auto-generated catch block
    20                 e.printStackTrace();
    21             }
    22         }
    23     }
    24 
    25 }
    Rose-实现runnable
     1 package ceshi.say;
     2 
     3 public class Jack extends Thread {
     4     private int num = 500;
     5 
     6     public void Jack() {
     7         this.num = num;
     8     }
     9 
    10     public void run() {
    11         for (int i = num - 1; i >= 0; i--) {
    12             Thread t = Thread.currentThread();
    13             System.out.println("Jack说:我是查水表");
    14 
    15             try {
    16                 Thread.sleep(1001);
    17             } catch (InterruptedException e) {
    18                 // TODO Auto-generated catch block
    19                 e.printStackTrace();
    20             }
    21         }
    22     }
    23 }
    Jack-继承Thread
     1 package ceshi.say;
     2 
     3 /**
     4  * 创建两个线程模拟两个人【一个实现runnable接口,一个继承thread类】, 分别叫jack和rose,模拟两人在对话1000次
     5  * 
     6  * @author L
     7  *
     8  */
     9 public class Main {
    10     public static void main(String[] args) {
    11         Jack j = new Jack();
    12         Rose r = new Rose();
    13         Thread jack = new Thread(j);
    14         Thread rose = new Thread(r);
    15 
    16         rose.start();
    17         jack.start();
    18     }
    19 
    20 }
    实现

    ——————————————————————————————————

    题目七

    使用List存储企鹅【penguin】信息

    需求说明:把多个企鹅的信息添加到集合中,查看企鹅的数量及所有企鹅的信息,删除集合中部分企鹅的元素,判断集合中是否包含指定企鹅

     1 package ceshi;
     2 
     3 public class Penguin {
     4     private String name;
     5     private String strain;
     6 
     7     public String getName() {
     8         return name;
     9     }
    10 
    11     public void setName(String name) {
    12         this.name = name;
    13     }
    14 
    15     public String getStrain() {
    16         return strain;
    17     }
    18 
    19     public void setStrain(String strain) {
    20         this.strain = strain;
    21     }
    22 
    23     public Penguin(String name, String strain) {
    24         super();
    25         this.name = name;
    26         this.strain = strain;
    27     }
    28 
    29 }
    企鹅类
     1 package ceshi;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 /**
     6  * 添加多个企鹅信息到集合中,查看企鹅数量及信息
     7  * 删除集合中的部分元素,判断集合中是否含有指定元素
     8  * @author L
     9  *
    10  */
    11 public class PenguinTest {
    12     public static void main(String[] args) {
    13 
    14         Penguin p1 = new Penguin("欧欧", "Q仔");
    15         Penguin p2 = new Penguin("亚亚", "Q妹");
    16         Penguin p3 = new Penguin("菲菲", "Q妹");
    17         Penguin p4 = new Penguin("美美", "Q妹");
    18         // 加入List
    19         List<Penguin> list = new ArrayList<>();
    20         list.add(p1);
    21         list.add(p2);
    22         list.add(p3);
    23         list.add(p4);
    24 
    25         System.out.println("共计有" + list.size() + "只企鹅。
    分别是:");
    26 
    27         for (Penguin penguin : list) {
    28             System.out.println(penguin.getName() + "	" + penguin.getStrain());
    29         }
    30         // 删除信息
    31         for (int i = 0; i < list.size(); i++) {
    32             if (list.get(i).getName().equals("菲菲")) {
    33                 list.remove(i);
    34             }
    35             if (list.get(i).getName().equals("美美")) {
    36                 list.remove(i);
    37             }
    38         }
    39         System.out.println("
    删除之后还有" + list.size() + "只企鹅。
    分别是:");
    40         for (Penguin penguin : list) {
    41             System.out.println(penguin.getName() + "	" + penguin.getStrain());
    42         }
    43         // 判断列表内是否有该内容
    44         String str = "美美";
    45         // boolean boo=false;
    46         for (Penguin penguin : list) {
    47             if (str.equals(penguin.getName())) {
    48                 System.out.println("集合包含" + str);
    49                 break;
    50             } else {
    51                 System.out.println("集合不包含" + str);
    52                 break;
    53             }
    54         }
    55     }
    56 }
    测试类

    ————————————————————————————————

    题目八

    需求说明:实现会员注册

    要求,用户名长度不小于3,密码长度不小于6,注册时两次输入密码必须相同

     1 package ceshi;
     2 
     3 import java.util.Scanner;
     4 
     5 /**
     6  * 实现注册功能,用户名不能小于3,密码不能小于6,两次密码要一致
     7  * 
     8  * @author L
     9  *
    10  */
    11 public class Login {
    12     static Scanner sc = new Scanner(System.in);
    13 
    14     public static void main(String[] args) {
    15         System.out.println("***欢迎来到注册系统***");
    16         System.out.println("请输入用户名:");
    17         String account = sc.next();
    18         while (account.length() < 3) {
    19             System.out.println("用户名长度不能小于3,请重新输入:");
    20             account = sc.next();
    21         }
    22         System.out.println("请输入注册密码:");
    23         String password = sc.next();
    24         for (; password.length() < 6;) {
    25             System.out.println("注册密码长度不能小于6,请重新输入!");
    26             password = sc.next();
    27         }
    28 
    29         System.out.println("请再次输入密码:");
    30         String password2 = sc.next();
    31         if (!password2.equals(password)) {
    32             System.out.println("您输入的密码有误,两次密码不一致,请重新输入:");
    33             password2 = sc.next();
    34         }
    35         System.out.println("注册成功!感谢您使用注册系统!");
    36     }
    37 }
    注册功能

    ————————————————————————————————

    题目九

    模拟用户登录的功能【网络编程】

    实现客户发送登录用户信息,服务器端显示登录信并响应给客户端登录成功 

     ————————————————————————————

    题目十

    复制图片,例如,把一个文件夹下的a.jpg复制成b.jpg

     1 package ceshi;
     2 
     3 import java.io.*;
     4 
     5 /**
     6  * 磁盘创建文件,复制该文件
     7  * 
     8  * @author L
     9  *
    10  */
    11 public class Picture {
    12     public static void main(String[] args) {
    13         DataInputStream dis = null;
    14         InputStream is = null;
    15 
    16         DataOutputStream dos = null;
    17         OutputStream os = null;
    18 
    19         try {
    20             is = new FileInputStream("D:\test\a.jpg");
    21             dis = new DataInputStream(is);
    22 
    23             os = new FileOutputStream("D:\test\b.jpg");
    24             dos = new DataOutputStream(os);
    25             int num;
    26             while ((num = dis.read()) != -1) {
    27                 dos.write(num);
    28             }
    29             System.out.println("复制完成!");
    30         } catch (FileNotFoundException e) {
    31             // TODO Auto-generated catch block
    32             e.printStackTrace();
    33         } catch (IOException e) {
    34             // TODO Auto-generated catch block
    35             e.printStackTrace();
    36         } finally {
    37             try {
    38                 dos.close();
    39                 os.close();
    40                 dis.close();
    41                 is.close();
    42             } catch (IOException e) {
    43                 // TODO Auto-generated catch block
    44                 e.printStackTrace();
    45             }
    46         }
    47     }
    48 }
    复制图片

  • 相关阅读:
    python之re(正则表达式)
    virtualbox之下载地址
    数据库之sqlite
    vmware 虚拟机扩展 liunx系统硬盘空间
    常见python面试题-手写代码系列
    Python的Struct模块
    mac上安装brew如何加速
    outlook数字证书导出、导入及解决老是弹出macOS需要访问系统钥匙串弹窗问题
    docker命令学习
    Message from syslogd不停的打日志
  • 原文地址:https://www.cnblogs.com/yanglanlan/p/11337252.html
Copyright © 2011-2022 走看看