目录
- 读取文本文件内容并打印
- 求文本文件中所有数据的平均数
- 求一个字符串中所有数字的和
- 通过两个线程打印希腊字母表和英语字母表
- 会计问题(线程同步)
- 将字符串"melon",“apple”,“pear”,"banana"按照字典顺序排列
- 选择排序算法
- 找素数
- 获取字符串中最大字符
- 字符串查重
- 判断字符串是否为回文串
- 查找数组中的最大值并返回最大值位置
- 统计字符串中每个字符出现的次数
- 读取文本文件
- 读取二进制文件
1、读取文本文件内容并打印
String fileName = "cost.txt", line;
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
line = in.readLine(); //读取一行内容
while (line != null) {
System.out.println(line);
line = in.readLine();
}
in.close();
} catch (IOException iox) {
System.out.println("Problem reading " + iox.getMessage());
}
2、求文本文件中所有数据的平均数
/*-----------------------
求文本文件中所有数据的平均数;
数据样例:
张三 34
李四 23
王五 123
韩六 442
郑七 12
-------------------------*/
File file = new File("C:\\Users\\52287\\IdeaProjects\\review\\src\\cost.txt");
Scanner sc = null;
int sum=0,count=0;
try {
sc = new Scanner(file);
sc.useDelimiter("[^0987654321.]+"); //scanner 设置分隔标记
while (sc.hasNext()) {
int price = sc.nextInt();
sum += price;
count++; // 计数
//.out.println(price); //输出price
}
System.out.println("和为"+sum);
System.out.println("平均为"+sum/count);
}catch (Exception exp){
System.out.println(exp);
}
3、求一个字符串中所有数字的和
/*
求一个字符串中所有数字的和
*/
//求菜单的所有价钱
String string = "北京烤鸭:189元,西芹炒肉:12.9元,酸菜鱼:69元,铁板牛柳:32元。";
Scanner scanner = new Scanner(string);
scanner.useDelimiter("[^0123456789.]+"); //非常重要的一行
double sum = 0;
int count = 0,index_1 = 0,index_2 = 0;
while (scanner.hasNext()){
try {
index_1 = string.indexOf(":",index_1+1);
double price = scanner.nextDouble();
sum = sum+price;
if (index_2 == 0){
System.out.println(string.substring(index_2,index_1+1)+price);
}else {
System.out.println(string.substring(index_2+1,index_1+1)+price);}
index_2 = string.indexOf(",",index_2+1);
}catch (InputMismatchException exp){
String t = scanner.next();
}
}
System.out.println("菜单共计"+sum+"元");
4、通过两个线程打印希腊字母表和英语字母表
public class Thread_java_review {
public static void main(String[] args) {
thread_1 p1 = new thread_1();
thread_2 p2 = new thread_2();
p1.start();
Thread p3 = new Thread(p2);
p3.start();
}
}
class thread_1 extends Thread{
public synchronized void run()
{
char a = 'a';
char b = 'z';
for(char i=a;i<=b;i++)
{
System.out.print(i);
}
}
}
class thread_2 implements Runnable{
public synchronized void run()
{
char a = 'α';
char b = 'ω';
for(char i=a;i<=b;i++)
{
System.out.print(i);
}
}
}
5、会计问题(线程同步)
//会计问题
/*
最终输出:
会计存入100,帐上有300万, 休息一会再存
会计存入100,帐上有400万, 休息一会再存
会计存入100,帐上有500万, 休息一会再存
出纳取出50帐上有450万,休息一会再取
出纳取出50帐上有400万,休息一会再取
出纳取出50帐上有350万,休息一会再取
*/
public class Thread_java_review {
public static void main(String[] args) {
Bank bank = new Bank();
bank.setMoney(200);
Thread accountant, cashier; //会计,出纳
accountant = new Thread(bank);
cashier = new Thread(bank);
accountant.setName("会计");
cashier.setName("出纳");
accountant.start();
cashier.start();
}
}
class Bank implements Runnable {
int money = 200;
public void setMoney(int n) {
money = n;
}
public void run() {
if (Thread.currentThread().getName().equals("会计"))
saveOrTake(300);
else if (Thread.currentThread().getName().equals("出纳"))
saveOrTake(150);
}
public synchronized void saveOrTake(int amount) { //存取方法
if (Thread.currentThread().getName().equals("会计")) {
for (int i = 1; i <= 3; i++) {
money = money + amount / 3; //每存入amount/3,稍歇一下
System.out.println(Thread.currentThread().getName() + "存入" + amount / 3 + ",帐上有" + money + "万, 休息一会再存");
try {
Thread.sleep(1000); //这时出纳仍不能使用saveOrTake方法
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else if (Thread.currentThread().getName().equals("出纳")) {
for (int i = 1; i <= 3; i++) {
money = money - amount / 3; //每取出amount/3,稍歇一下
System.out.println(Thread.currentThread().getName() + "取出" + amount / 3 + "帐上有" + money + "万,休息一会再取");
try {
Thread.sleep(1000); //这时会计仍不能使用saveOrTake方
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
6、将字符串"melon",“apple”,“pear”,"banana"按照字典顺序排列
//将字符串"melon","apple","pear","banana"按照字典顺序排列
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Fanxing_java_review {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("melon");
list.add("apple");
list.add("pear");
list.add("banana");
Collections.sort(list);
for(int i=0;i<list.size();i++)
System.out.println(list.get(i));
}
}
7、选择排序算法
//选择排序算法
public static void main(String[] args) {
int arr[] = {6, 5, 3, 2, 4};
//选择
for (int i = 0; i < arr.length; i++) {
//默认第一个是最小的。
int min = arr[i];
//记录最小的下标
int index = i;
//通过与后面的数据进行比较得出,最小值和下标
for (int j = i + 1; j < arr.length; j++) {
if (min > arr[j]) {
min = arr[j];
index = j;
}
}
//然后将最小值与本次循环的,开始值交换
int temp = arr[i];
arr[i] = min;
arr[index] = temp;
//说明:将i前面的数据看成一个排好的队列,i后面的看成一个无序队列。每次只需要找无需的最小值,做替换
}
}
8、找素数
//找素数
private static void testSushu() {
List<String> sushuList = new ArrayList<String>();
int num = 1000;
int j;
boolean isSushu;
for (int i = 2; i <= num; i++) {
//备注:这里加上这个判断其实有些时候也不一定会提升性能很明显,加了判断本身也是需要消耗计算机资源。数量比较大,提升会比较明显
if(i == 1 || (i % 2 == 0 && i != 2 ) ) continue; //偶数和1排除
isSushu = true;
for (j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isSushu = false;
break;
}
}
if (isSushu) {
sushuList.add(i+"");
}
}
System.out.println("数量:" + sushuList.size());
System.out.print("元素:");
for (String str : sushuList) {
System.out.print( str + " ");
}
}
9、获取字符串中最大字符
//获取字符串中最大字符
public class String_Max {
private static char getMax(String s){
char[] chars = new char[s.length()];
s.getChars(0,s.length(),chars,0);
sort(chars);
return chars[0];
}
private static void sort(char[] chars) {
//int count = 0;
for (int i = 0;i<chars.length;i++){
for (int j = i+1;j<chars.length;j++){
if ((chars[j]-chars[i])>0){
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
}
}
public static void main(String[] args) {
String s = new String();
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一行字符串:");
s = scanner.next();
System.out.println('\n'+"字符串中最大字符为"+getMax(s));
}
}
10、字符串查重
//字符串查重
String str = "我爱JAVA";
String str_fin = "";
for (int i=0;i<str.length();i++){
char ch=str.charAt(i);
if (str.indexOf(ch)==i){
str_fin = str_fin.concat(String.valueOf(ch)); //去重后的字符串
}
}
System.out.println(str);
System.out.println(str_fin);
11、判断字符串是否为回文串
//判断字符串是否为回文串
Scanner sca=new Scanner(System.in);
String str=sca.nextLine();
int a=str.length();
int middle=a/2,i;
for(i=0;i<middle&&str.charAt(i)==str.charAt(a-1-i);i++) {}
if(i<middle)
System.out.println(str+"不是回文");
else
System.out.println(str+"是回文");
12、查找数组中的最大值并返回最大值位置
//查找数组中的最大值并返回最大值位置
int []b= {1,2,3,4,5,6,7,8};
int max=b[0],p=0;
for(int i=1;i<b.length;i++) {
if(max<b[i]) {
max=b[i];
p=i;
}
}
System.out.println();
System.out.println("最大值为:"+max);
System.out.println("最大值在数组中的位置为:a["+p+"]"+",数组中第"+(p+1)+"个元素");
13、统计字符串中每个字符出现的次数
//统计字符串中每个字符出现的次数
//将字符串转化为字符数组
String str = "aaaaabbbbssssbsbsbsbddaaaaabbnsns";
char[] chars = str.toCharArray();
//创建一个HashMap名为hm
HashMap<Character, Integer> hm = new HashMap();
//定义一个字符串c,循环遍历遍历chars数组
for (char c : chars) {
//containsKey(c),当c不存在于hm中
if (!hm.containsKey(c)) {
hm.put(c, 1);
} else {
//否则获得c的值并且加1
hm.put(c, hm.get(c) + 1);
}
//或者上面的if和else替换成下面这一行
/* hm.put(c,hm.containsKey(c) ? hm.get(c)+1:1);*/
}
for (Character key : hm.keySet()) {
//hm.keySet()代表所有键的集合,进行格式化输出
System.out.println(key + "====" + hm.get(key));
}
14、读取文本文件
//读取文本文件
try {
FileReader fr=new FileReader("d:\\hello.txt");
char[] cbuf=new char[32];
int hasRead=0;
while((hasRead=fr.read(cbuf))>0){
System.out.println(new String(cbuf,0,hasRead));
}
} catch (Exception e) {
e.printStackTrace();
}
15、读取二进制文件
//读取二进制文件
try{
FileInputStream fis=new FileInputStream("d:\\hello.txt");
byte[] bbuf=new byte[1024];
int hasRead=0;
while((hasRead=fis.read(bbuf))>0){
System.out.println(new String(bbuf,0,hasRead));
}
fis.close();
}
catch (Exception e) {
e.printStackTrace();
}
复习的时候为了应付考试做的 ,可能会有错误,读者请自行改正~