Java语法基础50题训练(下)

题目1:
HashMap集合存储学生对象并遍历。

需求:
创建一个HashMap集合,键是学生对象(Student),值是居住地(String)。存储多个键值对象,并遍历。

要求:
保证键的唯一性:如果学生对象的成员变量值相同,我们就认为是同一个对象。

代码如下:

package HashMapPrintPack02;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;if (age != student.age) return false;return name != null ? name.equals(student.name) : student.name == null;}@Overridepublic int hashCode() {int result = name != null ? name.hashCode() : 0;result = 31 * result + age;return result;}
}
package HashMapPrintPack02;import java.util.HashMap;
import java.util.Set;public class HashMapDemo {public static void main(String[] args){HashMap<Student,String> hs = new HashMap<Student,String>();Student s1 = new Student("Tom",30);Student s2 = new Student("Jack",28);Student s3 = new Student("Lily",17);Student s4 = new Student("Tom",30);hs.put(s1,"西安");hs.put(s2,"北京");hs.put(s3,"上海");hs.put(s4,"深圳");Set<Student> keySet = hs.keySet();for (Student key:keySet){String value = hs.get(key);System.out.println(key.getName()+","+key.getAge()+","+value);}}}

题目2:
ArrayList集合存储HashMap元素并遍历。

需求:
创建一个ArrayList集合,存储三个元素,每一个元素都是HashMap,每一个HashMap的键和值都是String,并遍历。

代码如下:

package ArrayListAndHashMapPack01;import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;public class ArrayListIncludeHashMapDemo {public static void main(String[] args){ArrayList<HashMap<String,String>> array = new ArrayList<HashMap<String, String>>();HashMap<String,String> hm1 = new HashMap<String,String>();hm1.put("Tom","Jack");hm1.put("Bom","Lily");array.add(hm1);HashMap<String,String> hm2 = new HashMap<String,String>();hm1.put("小明","小红");hm1.put("小兰","小绿");array.add(hm2);HashMap<String,String> hm3 = new HashMap<String,String>();hm1.put("Mike","Zliy");hm1.put("Bo","Db");array.add(hm3);for(HashMap<String,String> hm:array){Set<String> keySet = hm.keySet();for (String key:keySet){String value = hm.get(key);System.out.println(key+","+value);}}}
}

题目3:
HashMap集合存储ArrayList元素并遍历。

需求:
创建一个HashMap集合,存储三个键值对元素,每一个键值对元素的键是String,值是ArrayList,每一个ArrayList的元素是String,并遍历。

代码如下:

package HashMapIncludeArrayListPack;import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;public class HashMapIncludeArrayListDemo {public static void main(String[] args){HashMap<String, ArrayList<String>> hm = new HashMap<String,ArrayList<String>>();ArrayList<String> sgyy = new ArrayList<String>();sgyy.add("赵云");sgyy.add("刘备");hm.put("三国演义",sgyy);ArrayList<String> xyj = new ArrayList<String>();xyj.add("唐僧");xyj.add("猴哥");hm.put("西游记",xyj);ArrayList<String> hlm = new ArrayList<String>();hlm.add("贾宝玉");hlm.add("林黛玉");hm.put("红楼梦",hlm);Set<String> keySet = hm.keySet();for (String key:keySet){System.out.println(key);ArrayList<String> value = hm.get(key);for (String s:value){System.out.println("\t"+s);}}}}

题目4:
统计字符串中每个字符串出现的次数。

需求:
键盘录入一个字符串,要求统计字符串中每个字符串出现的次数。
举例:
键盘录入"aababcabcdabcde",在控制台输出:“a(5)b(4)c(3)d(2)e(1)”

代码如下:

package CountStringNumPack;import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;public class HashMapDemp {public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串");String line = sc.nextLine();HashMap<Character,Integer> hm = new HashMap<Character,Integer>();for (int i  = 0;i<line.length();i++){char key = line.charAt(i);Integer value = hm.get(key);if (value==null){hm.put(key,1);}else {value++;hm.put(key, value);}}StringBuilder sb = new StringBuilder();Set<Character> keySet = hm.keySet();for (Character key:keySet){Integer value = hm.get(key);sb.append(key).append("(").append(value).append(")");}String ans = sb.toString();System.out.println(ans);}}
package CountStringNumPack;import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;public class HashMapDemp {public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串");String line = sc.nextLine();TreeMap<Character,Integer> hm = new TreeMap<Character,Integer>();for (int i  = 0;i<line.length();i++){char key = line.charAt(i);Integer value = hm.get(key);if (value==null){hm.put(key,1);}else {value++;hm.put(key, value);}}StringBuilder sb = new StringBuilder();Set<Character> keySet = hm.keySet();for (Character key:keySet){Integer value = hm.get(key);sb.append(key).append("(").append(value).append(")");}String ans = sb.toString();System.out.println(ans);}}

题目5:
ArrayList存储学生对象并排序。

需求:
ArrayList存储学生对象,使用Collections对ArrayList进行排序。
要求:
按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序。

代码如下:

package CollectionsDemo02;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
package CollectionsDemo02;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;public class CollectionsDemo02 {public static void main(String[] args){ArrayList<Student> array = new ArrayList<Student>();Student s1 = new Student("Tom",18);Student s2 = new Student("Jack",20);Student s3 = new Student("Bom",29);Student s4 = new Student("Lily",23);array.add(s1);array.add(s2);array.add(s3);array.add(s4);Collections.sort(array, new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {int num = s1.getAge()-s2.getAge();int num2 = num==0?s1.getName().compareTo(s2.getName()):num;return num2;}});for (Student s:array){System.out.println(s.getName()+","+s.getAge());}}
}

题目6:
模拟斗地主。

需求:
通过程序实现斗地主过程中的洗牌,发牌和看牌。

代码如下:

package PokerPack01;import java.util.ArrayList;
import java.util.Collections;public class PokerDemo01 {public static void main(String[] args) {ArrayList<String> array = new ArrayList<String>();String[] colors = {"◆", "♣", "♥", "♠"};String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};for (String color : colors) {for (String number:numbers){array.add(color+number);}}array.add("大王") ;array.add("小王");Collections.shuffle(array);//        System.out.println(array);ArrayList<String> tomArray = new ArrayList<String>();ArrayList<String> jackArray = new ArrayList<String>();ArrayList<String> bomArray = new ArrayList<String>();ArrayList<String> dpArray = new ArrayList<String>();for (int i  = 0;i<array.size();i++){String poker = array.get(i);if (i >= array.size()-3){dpArray.add(poker);}else if (i % 3==0){tomArray.add(poker);}else if (i % 3==1){jackArray.add(poker);}else if (i % 3==2){bomArray.add(poker);}}lookPoker("Tom",tomArray);lookPoker("Jack",jackArray);lookPoker("Bom",bomArray);}public static void lookPoker(String name,ArrayList<String> array){System.out.println(name+"的牌是:");for (String poker:array){System.out.print(poker+" ");}System.out.println();}
}

题目7:
模拟斗地主2.0

需求:
通过程序实现斗地主过程中的洗牌,发牌和看牌。
要求:
对牌进行排序。

代码如下:
有时间写!!!

题目8(递归):
有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?

代码如下:

package DiGuiPack;public class DiGuDemo01 {public static void main(String[] args){int ans =  f(20);System.out.println(ans);}public static int f(int n){if (n==1 || n==2) return 1;else return f(n-1)+f(n-2);}
}

题目9:

需求:
用递归求5的阶乘,并把结果在控制台输出。

代码如下:

package DiGuiPack;public class DiGuDemo01 {public static void main(String[] args){int ans =  f(5);System.out.println(ans);}public static int f(int n){if (n==1) return 1;else return n*f(n-1);}}

题目10:
遍历目录。

需求:
给定一个路径(D:\JavaDemo),请通过递归完成遍历该目录下的所有内容,并把所有文件的绝对路径输出在控制台。

代码如下:

package DiGuiPack;import java.io.File;public class DiGuiDemo02 {public static void main(String[] args){File srcfile = new File("D:\\JavaDemo");getAllFilePath(srcfile);}public static void getAllFilePath(File srcFile){File[] fileArray = srcFile.listFiles();if (fileArray!=null){for (File file:fileArray){if (file.isDirectory()){getAllFilePath(file);}else{System.out.println(file.getAbsolutePath());}}}}}

题目11:
复制文本文件。

需求:
把“D:\JavaDemo\java.txt”复制到模块目录下的"窗里窗外.txt"

代码如下:

package InstreamPack;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class CopyTxtDemo {public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("D:\\JavaDemo\\java.txt");FileOutputStream fos = new FileOutputStream("D:\\JavaDemo\\窗里窗外.txt");int by;while((by = fis.read())!=-1){fos.write(by);}fis.close();fos.close();}}

题目12:
复制图片。

需求:
把"D:\JavaDemo\luf.jpg",复制到某个目录。

代码如下:

package InstreamPack;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class FileInputStreamDemo02 {public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("D:\\JavaDemo\\luf.jpg");FileOutputStream fos = new FileOutputStream("D:\\JavaDemo\\tyc.jpg");byte[] bys = new byte[1024];int len;while((len=fis.read(bys))!=-1){fos.write(bys,0,len);}fis.close();fos.close();}
}

题目13:
复制视频。

需求:
把"D:\JavaDemo\字节流复制图片.avi",复制到模块目录下的"字节流复制图片.avi"

代码如下:

题目14:

复制Java文件。

需求:把“D:\JavaDemo\ArgsDemo01.java”复制一份。

代码如下:

package CopyJavaPack01;import java.io.*;public class CopyJavaDemo01 {public static void main(String[] args) throws IOException {InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\JavaDemo\\ArgsDemo01.java"));OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\JavaDemo\\hello.java"));int ch;while((ch = isr.read())!=-1){osw.write(ch);}osw.close();isr.close();}
}
package CopyJavaPack01;import java.io.*;public class CopyJavaDemo01 {public static void main(String[] args) throws IOException {InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\JavaDemo\\ArgsDemo01.java"));OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\JavaDemo\\hello.java"));//        int ch;
//        while((ch = isr.read())!=-1)
//        {
//            osw.write(ch);
//        }char[] chs  = new char[1024];int len;while((len=isr.read(chs))!=-1){osw.write(chs,0,len);}osw.close();isr.close();}
}

题目15:
复制Java文件。

需求:
把“D:\JavaDemo\ArgsDemo01.java”复制一份。

在这里插入图片描述

代码如下:

package CopyJavaPack01;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class CopyJavaDemo02 {public static void main(String[] args) throws IOException {FileReader fr = new FileReader("D:\\JavaDemo\\ArgsDemo01.java");FileWriter fw = new FileWriter("D:\\JavaDemo\\hellow02.java");int ch;while((ch=fr.read())!=-1){fw.write(ch);}fw.close();fr.close();}}
package CopyJavaPack01;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class CopyJavaDemo02 {public static void main(String[] args) throws IOException {FileReader fr = new FileReader("D:\\JavaDemo\\ArgsDemo01.java");FileWriter fw = new FileWriter("D:\\JavaDemo\\hellow02.java");//        int ch;
//        while((ch=fr.read())!=-1)
//        {
//            fw.write(ch);
//        }char[]chs = new char[1024];int len;while((len=fr.read(chs))!=-1){fw.write(chs,0,len);}fw.close();fr.close();}}

题目16:
复制Java文件(字符缓冲流改进版)

需求:
把“D:\JavaDemo\hellow02.java”复制一份。

代码如下:

package CopyJavaPack01;import java.io.*;public class CopyJavaDemo03 {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\hellow02.java"));BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\hello03.java"));int ch;while((ch=br.read())!=-1){bw.write(ch);}bw.close();br.close();}}
package CopyJavaPack01;import java.io.*;public class CopyJavaDemo03 {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\hellow02.java"));BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\hello03.java"));//        int ch;
//        while((ch=br.read())!=-1)
//        {
//            bw.write(ch);
//        }char [] chs = new char[1024];int len;while((len=br.read(chs))!=-1){bw.write(chs,0,len);}bw.close();br.close();}}

题目17:
复制Java文件(字符缓冲流特有功能改进版)

需求:
把“D:\JavaDemo\hello.java”复制一份。

package CopyJavaPack01;import java.io.*;public class CopyJavaDemo04 {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\hello.java"));BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\java01.java"));String line;while((line=br.readLine())!=null){System.out.println(line);bw.newLine();bw.flush();}br.close();bw.close();}}

题目18:
集合到文件。

需求:
把ArrayList集合中的字符串数据写入到文本文件。

要求:
每一个字符串元素作为文件中的一行数据。

代码如下:

package test22;import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;public class ArrayListToTxtDemo {public static void main(String[] args) throws IOException {ArrayList<String> array  = new ArrayList<String>();array.add("hello");array.add("world");array.add("java");BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\array.txt"));for (String s:array){bw.write(s);bw.newLine();bw.flush();}bw.close();}
}

题目19:
文件到集合。

需求:
把文本文件中的数据读取到集合中,并遍历集合。

要求:
文件中每一行数据是一个集合元素。

代码如下:

package test22;import java.io.*;
import java.util.ArrayList;public class TxtToArrayListDemo {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\array.txt"));ArrayList<String> array = new ArrayList<String>();String line;while((line = br.readLine())!=null){array.add(line);}br.close();for (String s:array){System.out.println(s);}}
}

题目20:
点名器。

需求:
我有一个文件里面存储了班级同学的姓名,每一行姓名占一行,要求通过程序实现随机点名器。

代码如下:

package test22;import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;public class CallNameDemo {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\array.txt"));ArrayList<String> array = new ArrayList<String>();String line;while((line = br.readLine())!=null){array.add(line);}br.close();Random r = new Random();int idx = r.nextInt(array.size());String name  = array.get(idx);System.out.println("幸运儿是:"+name);}}

题目21:
集合到文件(改进版)。

需求:
把ArrayList集合中的字符串数据写入到文本文件。

要求:
每一个字符串元素作为文件中的一行数据。

格式:
学号,姓名,年龄,居住地

举例:
001,Tom,24,北京

代码如下:

package test22;public class Student {private String sid;private String name;private int age;private String address;public Student() {}public Student(String sid, String name, int age, String address) {this.sid = sid;this.name = name;this.age = age;this.address = address;}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
package test22;import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;public class ArrayListToFileDemo {public static void main(String[] args) throws IOException {ArrayList<Student> array = new ArrayList<Student>();Student s1 = new Student("001","Tom",20,"北京");Student s2 = new Student("002","Jack",18,"上海");Student s3 = new Student("003","Bom",22,"韶关");Student s4 = new Student("004","Lily",23,"美国");array.add(s1);array.add(s2);array.add(s3);array.add(s4);BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\students.txt"));for (Student s:array){StringBuilder sb = new StringBuilder();sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());bw.write(sb.toString());bw.newLine();bw.flush();}bw.close();}}

题目22:
文件到集合(改进版)。

需求:
把文本文件中的数据读取到集合中,并遍历集合。

要求:
文件中每一行数据是一个集合元素。

举例:
001,Tom,29,北京

代码如下:

package test22;public class Student {private String sid;private String name;private int age;private String address;public Student() {}public Student(String sid, String name, int age, String address) {this.sid = sid;this.name = name;this.age = age;this.address = address;}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
package test22;import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;public class FileToArrayListDemo {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\students.txt"));ArrayList<Student> array = new ArrayList<Student>();String line;while((line = br.readLine())!=null){String[] strArray = line.split(",");Student s = new Student();s.setSid(strArray[0]);s.setName(strArray[1]);s.setAge(Integer.parseInt(strArray[2]));s.setAddress(strArray[3]);array.add(s);}br.close();for (Student s:array){System.out.println(s.getSid()+","+s.getName()+","+s.getAge()+","+s.getAddress());}}
}

题目23:
集合到文件(数据排序改进版)

需求:
键盘录入5个学生信息(学生,语文成绩,数学成绩,英语成绩)。要求按照成绩总分从高到低写入文本文件。

格式:
姓名,语文成绩,数学成绩,英语成绩

举例:
Tom,98,99,100

代码如下:

package ArrayListToFilePack;public class Student {private String name;private int chinese;private int math;private int english;public Student() {}public Student(String name, int chinese, int math, int english) {this.name = name;this.chinese = chinese;this.math = math;this.english = english;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChinese() {return chinese;}public void setChinese(int chinese) {this.chinese = chinese;}public int getMath() {return math;}public void setMath(int math) {this.math = math;}public int getEnglish() {return english;}public void setEnglish(int english) {this.english = english;}public int getSum(){return chinese+math+english;}}
package ArrayListToFilePack;import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;public class TreeSetToFileDemo {public static void main(String[] args) throws IOException {TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {int num1 = s2.getSum()-s1.getSum();int num2 = num1==0? s1.getChinese()- s2.getChinese() :num1;int num3 = num2==0?s1.getMath()-s2.getMath():num2;int num4 = num3==0?s1.getName().compareTo(s2.getName()):num3;return num4;}});for (int i  =0;i<5;i++){Scanner sc = new Scanner(System.in);System.out.println("请录入第"+(i+1)+"个学生信息");System.out.println("name = ");String name = sc.nextLine();System.out.println("语文成绩 = ");int chinese = sc.nextInt();System.out.println("数学成绩 = ");int math = sc.nextInt();System.out.println("英语成绩 = ");int english = sc.nextInt();Student s = new Student();s.setName(name);s.setChinese(chinese);s.setMath(math);s.setEnglish(english);ts.add(s);}BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo\\ts.txt"));for (Student s :ts){StringBuilder sb = new StringBuilder();sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.getSum());bw.write(sb.toString());bw.newLine();bw.flush();}bw.close();}
}

题目24:
复制单级文件夹。

在这里插入图片描述

需求:
把"D:\JavaDemo\hello"这个文件夹复制到"D:\JavaDemo02"这个文件夹里。

代码如下:

package CopyFolderPack;import java.io.*;public class CopyFolderDemo {public static void main(String[] args) throws IOException {File srcFolder = new File("D:\\JavaDemo\\hello");String  srcFolderName = srcFolder.getName();File destFolder = new File("D:\\JavaDemo02",srcFolderName);if (!destFolder.exists()){destFolder.mkdir();}File[] listFiles = srcFolder.listFiles();for (File srcFile : listFiles){String srcFileName = srcFile.getName();File destFile = new File(destFolder,srcFileName);copyFile(srcFile,destFile);}}private static void copyFile(File srcFile, File destFile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));byte[] bys = new byte[1024];int len;while((len = bis.read())!=-1){bos.write(bys,0,len);}bos.close();bis.close();}
}

题目25:
复制多级文件夹。

在这里插入图片描述

需求:
把"D:\JavaDemo"复制到"D:\JavaDemo02"这个文件夹里。

代码如下:

package CopyFolderPack;import java.io.*;public class CopyFoldersDemo {public static void main(String[] args) throws IOException {File srcFile = new File("D:\\JavaDemo");File destFile =  new File("D:\\JavaDemo02");copyFolder(srcFile,destFile);}//复制文件夹private static void copyFolder(File srcFile, File destFile) throws IOException {if (srcFile.isDirectory()){String srcFileName = srcFile.getName();File newFolder = new File(destFile,srcFileName);if (!newFolder.exists()){newFolder.mkdir();}File[] fileArray = srcFile.listFiles();for (File file:fileArray){copyFolder(file,newFolder);}}else{File newFile = new File(destFile,srcFile.getName());copyFile(srcFile,newFile);}}private static void copyFile(File srcFile, File destFile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));byte[] bys = new byte[1024];int len;while((len = bis.read(bys))!=-1){bos.write(bys,0,len);}bos.close();bis.close();}}

题目26:
复制Java文件(打印流改进版)

需求:
把"D:\JavaDemo\hello.java"复制到"D:\JavaDemo03\java01.java"

代码如下:

package CopyJavaPack02;import java.io.*;public class CopyJavaDemo {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\hello.java"));BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\JavaDemo03\\java01.java"));String line;while((line = br.readLine())!=null){bw.write(line);bw.newLine();bw.flush();}bw.close();br.close();}
}
package CopyJavaPack02;import java.io.*;public class CopyJavaDemo {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("D:\\JavaDemo\\hello.java"));PrintWriter bw = new PrintWriter(new FileWriter("D:\\JavaDemo03\\java01.java"),true);String line;while((line = br.readLine())!=null){bw.println(line);}bw.close();br.close();}
}

题目27:
游戏次数。

需求:
请写程序实现猜数字小游戏只能试玩3次,如果还想玩,提示:游戏试玩已结束,想玩请充值。

game.txt:

在这里插入图片描述

代码如下:

在这里插入代码片package PropertiesPack;import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;public class PropertiesTest {public static void main(String[] args) throws IOException {Properties prop = new Properties();FileReader fr = new FileReader("D:\\JavaDemo\\game.txt");prop.load(fr);fr.close();String count = prop.getProperty("count");int number = Integer.parseInt(count);if (number >= 3){System.out.println("游戏试玩已结束,想玩请充值");}else{GuessNumber.start();number++;prop.setProperty("count",String.valueOf(number));FileWriter fw = new FileWriter("D:\\JavaDemo\\game.txt");prop.store(fw,null);fw.close();}}}

题目28:
卖票。

需求:
某电影院目前正在上映国产大片,共有100张票,而它有3个窗口卖票,请设计一个程序模拟该电影院卖票。

在这里插入图片描述

代码如下:

package SellTicketPack;public class SellTicket implements Runnable{private int tickets = 100;@Overridepublic void run() {while (true) {if (tickets > 0) {System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");tickets--;}}}}
package SellTicketPack;public class SellTicketDemo {public static void main(String[] args){SellTicket st = new SellTicket();Thread t1 = new Thread(st,"窗口1");Thread t2 = new Thread(st,"窗口2");Thread t3 = new Thread(st,"窗口3");t1.start();t2.start();t3.start();}}

题目29:
卖票问题(进阶)。

点击下方链接:

卖票问题(进阶)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/310302.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用long类型让我出了次生产事故,写代码还是要小心点

昨天发现线上试跑期的一个程序挂了&#xff0c;平时都跑的好好的&#xff0c;查了下日志是因为昨天运营跑了一家美妆top级淘品牌店&#xff0c;会员量近千万&#xff0c;一下子就把128G的内存给爆了&#xff0c;当时并行跑了二个任务&#xff0c;没辙先速写一段代码限流&#x…

Mongodb查询分析器解析

Mongodb查询分析器 动态相关项目中涉及到数据量大和吞吐量的接口&#xff0c;例如关注页面动态&#xff0c;附近动态&#xff0c;这部分数据都是存储在mongodb中&#xff0c;在线上数据中分类两个mongodb集合存储其中关注动态基于扩散写的设计&#xff0c;数据量已经快到 8 亿…

[Java基础]Collections概述和使用

代码如下: package CollectionDemo01;import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List;public class CollectionDemo01 {public static void main(String[] args){List<Integer> list new ArrayList&l…

链路追踪在ERP系统中的应用实践

源宝导读&#xff1a;随着ERP的部署架构越来越复杂&#xff0c;对运维监控、问题排查等工作增加了难度&#xff0c;本文将介绍通过引入链路追踪技术&#xff0c;提高ERP系统问题排查效率&#xff0c;支撑更全面监控系统运行情况的实践过程。一、导读随着ERP的部署架构越来越复杂…

[Java基础]File基础

File类概述和构造方法: 代码如下: package FileStudyPack;import java.io.File;public class FileDemo01 {public static void main(String[] args){File f1 new File("D:\\JavaDemo\\java.txt");System.out.println(f1);File f2 new File("D:\\JavaDemo&quo…

java 日志乱码_【开发者成长】JAVA 线上故障排查完整套路!

云栖号资讯&#xff1a;【点击查看更多行业资讯】在这里您可以找到不同行业的第一手的上云资讯&#xff0c;还在等什么&#xff0c;快来&#xff01;线上故障主要会包括 CPU、磁盘、内存以及网络问题&#xff0c;而大多数故障可能会包含不止一个层面的问题&#xff0c;所以进行…

谈谈登录密码传输这件小事

背景 大大小小的系统其实都离不开登录这个小小的功能&#xff0c;前段时间老黄在审查公司部分系统代码时&#xff0c;发现不少系统的登录还是很粗暴的&#xff0c;粗暴到让人不敢说话的那种。说到登录&#xff0c;结合标题&#xff0c;其实大部分人应该都猜到那个粗暴到让人不敢…

技术分享杂七杂八技术

技术分享 听花谷 距离名宿 6~7 公里左右&#xff0c;丽江网红基地&#xff0c;有举办婚礼的地方听花谷&#xff0c;坐落于玉龙雪山脚下&#xff0c;前有玉龙雪山&#xff0c;后有原始森林。园内共有三处白色空间&#xff0c;第一处共有三层&#xff0c;婚礼举行&#xff0c;发…

java 操作日志设计_日志系统新贵 Loki,确实比笨重的ELK轻

本文同步Java知音社区&#xff0c;专注于Java作者&#xff1a;linkt1234http://blog.csdn.net/Linkthaha/article/details/100575278最近&#xff0c;在对公司容器云的日志方案进行设计的时候&#xff0c;发现主流的ELK或者EFK比较重&#xff0c;再加上现阶段对于ES复杂的搜索功…

Istio1.5 Envoy 数据面 WASM 实践

Istio 1.5 回归单体架构&#xff0c;并抛却原有的 out-of-process 的数据面扩展方式&#xff0c;转而拥抱基于 WASM 的 in-proxy 扩展&#xff0c;以期获得更好的性能。本文基于网易杭州研究院轻舟云原生团队的调研与探索&#xff0c;介绍 WASM 的社区发展与实践。超简单版解释…

elasticSearch -- (文档,类型,索引)

问题:大规模数据如何检索 当系统数据量达到10亿&#xff0c;100亿级别的时候&#xff0c;我们系统该如何去解决这种问题。 数据库选择—mysql&#xff0c; sybase&#xff0c;oracle&#xff0c;mongodb&#xff0c;hbase…单点故障如何解决—lvs&#xff0c; F5&#xff0c;…

asp后台调用产品数据_后台产品经理,需掌握这些数据交互知识

人们每天都在接收信息和发送信息&#xff0c;在传递信息的过程中&#xff0c;明白对方要表达的意思。数据也是如此&#xff0c;在系统交换数据的过程中&#xff0c;就伴随着数据交互。本篇文章将为大家具体分析前端和后台的数据交互与协议。本文所说的”数据交换” 是指在计算机…

使用c# .net core开发国标gb28181 sip +流媒体服务完成视频监控实例教程 亲身完美体验过程...

目前使用C# .net core 来实现国标gb28181标准的摄像头播放、ptz云台控制、视频回放等视频监控功能&#xff0c;项目可运行于linux/docker/.net core环境&#xff0c;也是当前非常罕有的能做到毫秒级国标gb28181公网视频传送案例&#xff0c;也是少有的能同时具有播放、ptz云台控…

[Java基础]字节流读数据

代码如下: package InstreamPack;import java.io.FileInputStream; import java.io.IOException;public class FileInputStreamDemo01 {public static void main(String[] args) throws IOException {FileInputStream fis new FileInputStream("D:\\JavaDemo\\java.txt&…

丁可以组什么词_有哪些量词可以用来描述生意经?

分别有&#xff1a;本&#xff0c; 一(本)生意经。笔&#xff0c; 一(笔)生意经。次&#xff0c;一(次)生意量词用名量词&#xff1a;表示事物的计量单位。基本定义&#xff1a;通常用来表示人、事物或动作的数量单位的词&#xff0c;叫做量词。量词 lingc&#xff0c;与代表可…

硬核技能k8s初体验

&#xff0c;Kubernetes 是一个软件系统&#xff0c;使你在数以万计的电脑节点上运行软件时就像所有节点是以单个大节点一样&#xff0c; 它将底层基础设施抽象&#xff0c;这样做同时简化了应用开发、部署&#xff0c;以及对开发和运维团队的管理。Kubernetes集群架构Kubernet…

线上问题排查流程

问题排查 针对各种常见的线上问题&#xff0c;梳理下排查思路。 业务问题 线上问题大多数时候都是业务问题引发的问题&#xff0c;当线上环境绝大多数请求都是正常&#xff0c;当有部分或者某一个用户有问题&#xff0c;此时怎么针对性的排查在当前微服务体系下&#xff0c;…