1.

需求:    1.    随机生成60-15(包含015)的数字,作为彩票的中奖号码,要求最终的中奖号码没有重复数字

        2.    键盘录入60-15(包含015)的数字,比较录入的数字和中奖号码相同的个数:

        小于3个,没有中奖

        3个一样,中三等奖

        4个一样,中二等奖

        5个一样,中一等奖

        当全部一样,中特等奖

        在控制台打印中奖结果,样式如下:

            “您的号码为XX,XX,XX,XX,XX,XX,中了X等奖,恭喜您!或者

            “您的号码为XX,XX,XX,XX,XX,XX,没有中奖!

package cn.itcast_01;

 

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

 

public
class Test {

 

    static ArrayList<Integer> list1 = new ArrayList<Integer>();

    public
static
void main(String[] args) {

        //创建产生随机数对象

        Random r= new Random();

        

        //创建一个存储随机数的集合

        ArrayList<Integer> list = new ArrayList<Integer>();

        

        // 定义一个统计变量。从0开始。

        int count = 0;

    
 

        //判断统计遍历是否小于6

        while(count<6){

            //先产生一个随机数

            int number = r.nextInt(16);

            

            //判断该随机数在集合中是否存在

            if(!list.contains(number)){

                list.add(number);

                count++;

            }

        }

        System.out.println(list);

        //创建键盘录入对象

        Scanner sc = new Scanner(System.in);

        //循环输入60-15(包含015)的数字

        int count1 =0;

        for (int i = 0; i < 6; i++) {

            System.out.println(“请输入第“+(i+1)+”个数字:“);

            int number1 = sc.nextInt();

            

            list1.add(number1);

            //与集合中数据进行比较

            for (int j = 0; j < list.size(); j++) {

                Integer integer = list.get(j);

                if(number1 == integer){

                    count1++;

                }

            }

            

        }

        

            String s =init();

        switch(count1){

            case 3:

                System.out.println(“您的号码为“+ s+ “中了三等奖,恭喜您!“);

                break;

            case 4:

                System.out.println(“您的号码为“+ s+ “中了二等奖,恭喜您!“);

                break;

            case 5:

                System.out.println(“您的号码为“+ s+ “中了一等奖,恭喜您!“);

                break;

            case 6:

                System.out.println(“您的号码为“+ s+ “中了特等奖,恭喜您!“);

                break;

            default :

                System.out.println(“您的号码为“+ s+ “没有中奖“);

                break;

                

        }

    }

    public
static String init(){

        String s =””;

        for (int i = 0; i < list1.size(); i++) {

            s = s+list1.get(i)+”,”;

        }

        return s;

    }

 

}

 

 

例2.

需求:    1.    项目根目录下已存在文件data.txt,文件中存放用户名和密码,存储格式如下(原始用户名和密码不用程序录入,可手动建文件并在文件中录入数据)

        用户名,密码(中间用,分隔),如:

        jack1234

        tom4567

2.    程序启动时:提示用户输入登录名和登录密码

            2.1 键盘录入用户名和密码

            2.2 data.txt中获取用户名和密码,验证键盘录入的用户名和密码是否和data.txt中用户名密码匹配,

            是:控制台提示:提示登录成功,并结束程序;

            否:控制台提示:提示登录失败,用户名或密码错误!,并结束程序

package cn.itcast_01;

 

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

 

public class Test02 {

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

        //创建键盘录入对象

        Scanner sc = new Scanner(System.in);

        System.out.println(“请输入用户名:”);

        String username = sc.nextLine();

        System.out.println(“请输入密码:”);

        String password = sc.nextLine();

        String s =username+”,”+password;

        

        //创建流对象

        BufferedReader br = new BufferedReader(new FileReader(“data.txt”));

        

        //获取文本数据

        String b ;

        int count=0;

        int count1 = 0;

        while((b = br.readLine())!=null){

            count++;

            if(s.equals(b)){

                System.out.println(“登录成功”);

            }else{

                count1++;

            }

        }

        if(count ==count1){

            System.out.println(“登录失败,用户名或密码错误!”);

        }

    }

}

 

例3.完成猜数字小游戏

需求:

        1:生成随机数1-100

        2:用户键盘录入一个数字,与该随机数进行比较

        3:如果猜数正确,退出程序

        4:如果猜大了,打印猜大了,请重新猜,用户继续猜

        5:如果猜小了,打印猜小了,请重新猜,用户继续猜

        6:一共可以猜6

        7:当用户6次都没猜中时,程序结束,将用户所猜过的所有结果打印到控制台

package cn.itcast_01;

 

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

 

import org.omg.Messaging.SyncScopeHelper;

 

public class Test03 {

    public static void main(String[] args) {

        //定义集合用于存储键盘录入的元素

        ArrayList<Integer> list = new ArrayList<Integer>();

        //创建获取随机数对象

        Random r = new Random();

        

        //生成随机数1-100

        int i1 = r.nextInt(100)+1;

        

        //创建键盘录入对象

        Scanner sc = new Scanner(System.in);

        System.out.println(“请输入你猜的数字(1-100):”);

        

        

        for (int i = 0; i < 6; i++) {

            //用户键盘录入一个数字,与该随机数进行比较

            int nextInt = sc.nextInt();

            list.add(nextInt);

            if(nextInt>i1){

                System.out.println(“猜大了,请重新猜”);

            }else if(nextInt<i1){

                System.out.println(“猜小了,请重新猜”);

            }else{

                System.out.println(“恭喜你猜对了!!!”);

                break;

            }

            System.out.println(“你还剩”+(5-i)+”次机会。”);

        }

        System.out.println(“你所猜过的所有数据为:”);

        System.out.println(list);

//        for (int i = 0; i < list.size(); i++) {

//            Integer in = list.get(i);

//            System.out.print(in+”,”);

//        }

    }

}

 

例4

需求: 1.在本地E:/numbers.txt,并添加如下两行数据:

        89,90,77,87,66,54,328,890,99

        65,72,12,77,2,96,54,27,89

     2.要求:编写程序读取此文件中的所有数字,并将重复的数字只保留一个写入另一个文件:E:/result.txt


每个数字中间用逗号隔开;


要求:最后一个数字后不能有逗号;

package cn.itcast_01;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.HashSet;

import java.util.Set;

 

public class Test04 {

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

        //创建高效输入流对象

        BufferedReader br = new BufferedReader(new FileReader(“E:/numbers.txt”));

        //创建高效输出流对象

        BufferedWriter bw = new BufferedWriter(new FileWriter(“E:/result.txt”));

        

        //创建HashSet集合存储数字

        Set<Integer> set = new HashSet<Integer>();

        

        //操作数据

        String s;

        while((s= br.readLine())!=null){

            //根据匹配给定的正则表达式来拆分此字符串

            String[] array = s.split(“,”);

            //for循环遍历

            for(String s1:array){

                //往集合中添加元素

                set.add(Integer.valueOf(s1));

            }

        }

        //关闭资源

        br.close();

        //创建字符串缓冲区对象

        StringBuffer b = new StringBuffer();

        //将集合中元素写到文本中

        for(Integer n:set){

            b.append(n).append(“,”);

        }

        

        //移除最后一个逗号

        b.deleteCharAt(b.length()-1);

        //写数据

        bw.write(b.toString());

        //关闭资源

        bw.close();

        

    }

}

 

例5.

需求:使用键盘录入一个字符串,获取字符串中每一个字符出现的次数,要求统计结果格式为:

    字符(次数)并以“,”连接,如:a(5),b(4),c(3),d(2),e(1)

    将统计结果在控制台打印并将统计结果保存到项目根目录下的result.txt文件中

package cn.itcast_04;

 

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Scanner;

import java.util.Set;

import java.util.TreeMap;

 

public class Test {

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

        // 创建键盘录入对象

        Scanner sc = new Scanner(System.in);

        System.out.println(“请输入您要判断的字符串: “);

 

        String str = sc.nextLine();

        // TreeMap集合根据其键的自然顺序进行排序

        Map<String, Integer> map = new TreeMap<String, Integer>();

 

        int len = 0;

 

        // 字符串为空时,停止循环

        while (str.length() > 0) {

 

            // 当前字符串的长度

            len = str.length();

 

            // 取第一个字符

            String s = str.substring(0, 1);

 

            // 字符串,把这个字符通篇换成空格

            str = str.replaceAll(s, “”);

 

            // 这个字符的次数就是 两个字符串长度的差

            map.put(s, len – str.length());

        }

        // 调用集合entry方法

        Set<Entry<String, Integer>> entrySet = map.entrySet();

        // 创建StringBuffer对象

        StringBuffer s = new StringBuffer();

        // 增强for循环遍历

        for (Entry<String, Integer> en : entrySet) {

 

            s.append(en.getKey()).append(“(“).append(en.getValue()).append(“),”);

        }

        String substring = s.substring(0, s.length() – 1);

        // 打印

        System.out.print(substring);

 

        // 创建输出流

        BufferedWriter bw = new BufferedWriter(new FileWriter(“result.txt”));

        // 写出数据

        bw.write(substring);

        // 关闭资源

        bw.close();

    }

}

 

例6

需求:1.定义类Employee,包含如下字段:

        姓名(String),性别(String),销售额:(整数)

     此类要求包含无参构造方法,全参构造方法,所有字段的getset方法;

    2.初始化如下几个Employee对象

        “张三“,,88907

        “李四“,,76895

        “王五“,,8900

        “周六“,,90000

        “赵七“,,79989

    3.将上述对象存储到HashMap集合中,要求使用泛型;

    4.要求:公司要求按照销售额计提奖金,提取标准:8万元(不含)以下按20%提取;8万元以上按25%提取;


请计算所有人员应发奖金,结果取整数,并按如下格式打印:


姓名        性别        销售额    计提百分比        奖金额


xxx            x            xxxx    xx%            xxxx

package cn.itcast_05;

/*

* Employee

*/

public
class Employee {

    //属性

    private String name;

    private String sex;

    private
int sales;

    //全参构造

    public Employee(String name, String sex, int sales) {

        super();

        this.name = name;

        this.sex = sex;

        this.sales = sales;

    }

    //无参构造

    public Employee() {

        super();

        // TODO Auto-generated constructor stub

    }

    //getset方法

    public String getName() {

        return name;

    }

    public
void setName(String name) {

        this.name = name;

    }

    public String getSex() {

        return sex;

    }

    public
void setSex(String sex) {

        this.sex = sex;

    }

    public
int getSales() {

        return sales;

    }

    public
void setSales(int sales) {

        this.sales = sales;

    }

    
 

    

}

package cn.itcast_05;

 

import java.util.HashMap;

import java.util.Map.Entry;

import java.util.Set;

 

public class Test {

    public static void main(String[] args) {

        // 创建Employee对象

        Employee e1 = new Employee(“张三”, “男”, 88907);

        Employee e2 = new Employee(“李四”, “男”, 76895);

        Employee e3 = new Employee(“王五”, “男”, 8900);

        Employee e4 = new Employee(“周六”, “男”, 90000);

        Employee e5 = new Employee(“赵七”, “女”, 79989);

        // 创建HashMap集合对象

        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();

        // 往集合中添加元素

        hashMap.put(e1.getName() + ”    ” + e1.getSex(), e1.getSales());

        hashMap.put(e2.getName() + ”    ” + e2.getSex(), e2.getSales());

        hashMap.put(e3.getName() + ”    ” + e3.getSex(), e3.getSales());

        hashMap.put(e4.getName() + ”    ” + e4.getSex(), e4.getSales());

        hashMap.put(e5.getName() + ”    ” + e5.getSex(), e5.getSales());

 

        Set<Entry<String, Integer>> entrySet = hashMap.entrySet();

 

        // 定义奖金

        int jiangJin = 0;

        // 定义计提百分比

        String s = “”;

        System.out.println(“姓名    性别    销售额    计提百分比    奖金额”);

        for (Entry<String, Integer> en : entrySet) {

            if (en.getValue() < 80000) {

                s = “20%”;

                jiangJin = (int) (en.getValue() * 0.20);

            } else {

                s = “25%”;

                jiangJin = (int) (en.getValue() * 0.25);

            }

            System.out.println(en.getKey() + ”    ” + en.getValue() + ”    ” + s + ”    ” + jiangJin);

        }

 

    }

}

例7.复制指定目录下指定后缀名的文件并修改文件名称

需求:将D:\aaa下的所有.txt文件复制到E:\aaa_copy,并且将.txt文件,重命名为.java

    1.    定义void copyFileAndRename(File srcFile, File destFile)静态方法,方法内要求:

    如果目标目录不存在需要创建目标目录

    完成将源目录中的文件复制到目标目录下,并将后缀名修改为.java

    每复制完并更改后缀之后提示哪个文件复制完毕

    2.main方法中定义
源目录和目标目录,调用copyFileAndRename方法,复制完在控制台提示复制完毕

package cn.itcast_06;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

 

public class Test {

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

        // 创建源文件对象

        File srcFile = new File(“D:\\aaa”);

        // 创建目标文件对象

        File destFile = new File(“E:\\aaa_copy”);

        // 调用复制方法

        copyFileAndRename(srcFile, destFile);

    }

 

    public static void copyFileAndRename(File srcFile, File destFile) throws IOException {

        // 如果目标文件夹不存在就创建

        if (!destFile.exists()) {

            destFile.mkdirs();

        }

        // 把源文件夹转为数组

        File[] listFiles = srcFile.listFiles();

        // for循环遍历

        for (File file : listFiles) {

            // 判断是文件还是文件夹

            // 如果是文件

            if (file.isFile()) {

                if (file.getName().endsWith(“.txt”)) {

                    String name = file.getName().replace(“.txt”, “.java”);

                    // 创建高效流对象

                    BufferedReader br = new BufferedReader(new FileReader(file));

                    BufferedWriter bw = new BufferedWriter(new FileWriter(new File(destFile, name)));

 

                    // 数据操作

                    int b;

                    while ((b = br.read()) != -1) {

                        bw.write(b);

                    }

                    // 关闭资源

                    bw.close();

                    br.close();

                    System.out.println(name + “复制完毕!”);

                } else {

                    copyFileAndRename(file, new File(destFile, file.getName()));

                }

            }

        }

    }

}

 

例8.

需求:1.    在项目根目录下新建一个文件:data.txt,键盘录入3个字符串验证码,并存入data.txt中,要求一个验证码占一行;

     2.    键盘录入一个需要被校验的验证码,如果输入的验证码在data.txt中存在:在控制台提示验证成功,如果不存在控制台提示验证失败

package cn.itcast_07;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

 

public class Test {

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

        // 创建键盘录入对象

        Scanner sc = new Scanner(System.in);

        // 创建输出流对象

        BufferedWriter bw = new BufferedWriter(new FileWriter(“dta.txt”));

        // 创建输入流对象

        BufferedReader br = new BufferedReader(new FileReader(“dta.txt”));

 

        for (int i = 0; i < 3; i++) {

            System.out.println(“请第” + (i + 1) + “次输入要存储的字符串验证码:”);

            String str = sc.nextLine();

            bw.write(str);

            bw.newLine();

        }

        bw.close();

        System.out.println(“请输入您的验证码: “);

        String line = sc.nextLine();

        // 操作数据

        String b;

        while ((b = br.readLine()) != null) {

            if (line.equals(b)) {

                System.out.println(“验证成功!”);

                return;

            }

        }

        // bw.close();//要注意关闭资源的位置,如果把关闭资源放到此处,则在判断的时候,三个字符串还未写出到文本中,故输出验证失败

        br.close();

        System.out.println(“验证失败!”);

 

    }

}

 

例9.

需求:请用集合知识点做出此题:黑马有很多专业,例如JAVA-学费19980,IOS-学费19980,PHP-10000,

网络营销-1000,C++-10000,UI-10000

A.添加一个前端(学费12000)课程

B.请将PHP学科的价格改为12000,不能直接赋值12000

C.将所有学科存放到当前的项目目录下,文件名自定义

package cn.itcast_10;

 

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map.Entry;

import java.util.Set;

 

public class Test {

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

        // 定义一个map集合,键和值分别存储学科名称和学费

        HashMap<String, Integer> map = new HashMap<String, Integer>();

        // 往集合中添加元素

        map.put(“JAVA”, 19980);

        map.put(“IOS”, 19980);

        map.put(“PHP”, 10000);

        map.put(” 网络营销”, 1000);

        map.put(“C++”, 10000);

        map.put(“UI”, 10000);

        // A.添加一个前端(学费12000)课程

        map.put(“前端”, 12000);

 

        // 定义输出流

        BufferedWriter bw = new BufferedWriter(new FileWriter(“cz.txt”));

 

        Set<Entry<String, Integer>> entrySet = map.entrySet();

        for (Entry<String, Integer> en : entrySet) {

            // B.请将PHP学科的价格改为12000元,不能直接赋值12000

            if (en.getKey().equals(“PHP”)) {

                en.setValue(12000);

            }

            String s = en.getKey() + “–” + en.getValue().toString();

            // 写出数据

            bw.write(s);

            bw.newLine();

        }

        // 关闭资源S

        bw.close();

    }

 

}

 

例10

需求:编写10个线程,第一个线程从1加到10,第二个线程从11加到20……

第十个线程从91加到100;最后把是个线程的结果相加

package cn.itcast_09;

 

import java.util.concurrent.Callable;

 

public
class MyCall implements Callable<Integer> {

    // 定义一个变量用于接收累加和的最大值

    int num;

 

    // 空参构造

    public MyCall() {

    }

 

    // 满参构造

    public MyCall(int num) {

        super();

        this.num = num;

    }

 

    int sum = 0;

 

    public Integer call() {

        for (int i = (num – 9); i <= num; i++) {

            sum += i;

        }

        System.out.println(Thread.currentThread().getName() + “计算从” + (num – 9) + “” + num + “的累加和为: ” + sum);

        return sum;

 

    }

}

package cn.itcast_09;

 

import java.util.concurrent.ExecutionException;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

 

public class Test {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        //获取线程池

        ExecutorService pool = Executors.newFixedThreadPool(10);

        //计算从1加到10的和

        Future<Integer> submit1 = pool.submit(new MyCall(10));

        Integer i1 = submit1.get();

        //计算从11加到20的和

        Future<Integer> submit2 = pool.submit(new MyCall(20));

        Integer i2 = submit2.get();

        

        //计算从21加到30的和

        Future<Integer> submit3 = pool.submit(new MyCall(30));

        Integer i3 = submit3.get();

        

        //计算从31加到40的和

        Future<Integer> submit4 = pool.submit(new MyCall(40));

        Integer i4 = submit4.get();

        

        //计算从41加到50的和

        Future<Integer> submit5 = pool.submit(new MyCall(50));

        Integer i5 = submit5.get();

        

        //计算从51加到60的和

        Future<Integer> submit6 = pool.submit(new MyCall(60));

        Integer i6 = submit6.get();

        

        //计算从61加到70的和

        Future<Integer> submit7 = pool.submit(new MyCall(70));

        Integer i7 = submit7.get();

        

        //计算从71加到80的和

        Future<Integer> submit8 = pool.submit(new MyCall(80));

        Integer i8 = submit8.get();

        

        //计算从81加到90的和

        Future<Integer> submit9 = pool.submit(new MyCall(90));

        Integer i9 = submit9.get();

        

        //计算从91加到100的和

        Future<Integer> submit10 = pool.submit(new MyCall(100));

        Integer i10 = submit10.get();

        

        System.out.println(“总和为:”+(i1+i2+i3+i4+i5+i6+i7+i8+i9+i10));

    }

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注