Java抱佛脚做练习(二)

1、分别用字节流,BufferedReader完成下面操作。
在当前目录下创建一个worldcup.txt 的文本文件,其格式如下:
2006/意大利
2002/巴西

该文件采用“年份/世界杯冠军”的方式保存每一年世界杯冠军的信息。
要求:读入该文件的基础上,让用户输入一个年份,输出该年的世界杯冠军。如果该年没有举办世界杯,则输出“没有举办世界杯”
(老师写的,说抛砖引玉,我是泥巴。。。)

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test8 {
 public static void main(String[] args) {
        BufferedReader br = null;
        List<String> contentList = new ArrayList<>();//保存读取的文件内容
        try {
            br = new BufferedReader(new FileReader("d:\\abc.txt"));
            String str = "";
            while ((str = br.readLine()) != null) {
                contentList.add(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("Please input year(xxxx):");
        String year = sc.next();
        boolean flag = false;//标志是否举办过世界杯
        for (String content : contentList) {
            if (content.contains(year)) {
                flag = true;
                System.out.println("冠军是:" + content.substring(content.indexOf("/") + 1));
                break;
            }
        }
        if (!flag) {
            System.out.println("该年没有举办世界杯!");
        }
    }
}

(我没接触过,怎么知道有content.substring(content.indexOf("/") + 1这种操作嘛?)

2、有一个文件“湖北工业大学.txt”,判断该文件内是否有字符串“湖工” 。

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class Test9 {
    public static void main(String[] args) {
       byte[] chs=new byte[1024];
        int len=0;
        String s ="";
        try( FileInputStream fr = new FileInputStream("D:\\湖北工业大学.txt")
        ){
            while((len=fr.read(chs))!=-1){
               String str=new String(chs,0,len);
               s=s+str;
            }
            s.replaceAll("\n","");
            System.out.println(s);
            if(s.contains("湖工")){
                System.out.println(true);
            }else{
                System.out.println(false);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

3、按要求编写Java程序:
(1)编写一个接口:Interface Itest,只含有一个方法int method( );(2)编写一个类:Clas A来实现接口Itest, int method( )方法时,要求计算1到100的和;
(3)编写另一个类:Class B来实现接口Itest, int method( )方法要求计算10的阶乘(n!);
(4)编写主体类E,方法void getResult要求输出Itest子类的method方法的结果,无论Itest有多少个子类,程序的主体代码都不用修改;(5)编写测试类Test,在测试类Test的main方法中测试实现接口的类。

interface Itest {
    int method();
}
class A implements Itest{
    @Override
    public int method() {
        int sum=0;
        for(int i=1;i<=100;i++){
            sum=sum+i;
        }
        return sum;
    }
}
class B implements Itest{
    @Override
    public int method() {
        int sum=1;
        for(int i=1;i<=10;i++){
            sum=sum*i;
        }
        return sum;
    }
}
class E {
   public void getResult(Itest it) {
        System.out.println(it.method());
    }
}
public class Test1 {
    public static void main(String[] args) {
        Itest ita = new A();
        Itest itb = new B();
        E e = new E();
        e.getResult(ita);
    }
}

4、17计算机班的B去参加校园歌手大奖赛,有5个评委打分,将评委的分数通过键盘输入(使用Scanner进行输入),要求定义一个数组score来保存这些分数,5个评委的分数为(97,98,99,98,100),去掉一个最高一个最低后求平均分.

import java.util.*;
public class Test2 {
    static Scanner s1=new Scanner(System.in);
    public static void main(String[] args) {
     int a[]=new int[6];
     a[0]=s1.nextInt();
     a[1]=s1.nextInt();
     a[2]=s1.nextInt();
     a[3]=s1.nextInt();
     a[4]=s1.nextInt();
     int max=0,min=100;
     int countm=0;
     int countn=0;
     for(int i=0;i<=4;i++) {
       if(a[i]>max) {
        max=a[i];
        countm=i;
       }
       if(a[i]<min) {
        min=a[i];
        countn=i;
       }
      }
     a[countm]=0;
     a[countn]=0;
     double sum=0;
     int count=0;
     for(int j=0;j<=4;j++) {
      if(a[j]!=0) {
       sum+=a[j];
       count++;
      }
     }
     System.out.println("平均分是"+sum/count);
     }
}

5、编程实现:计算机学院王书记到课堂查考勤。要求:
a、有一个接口Kaoqinable,接口有一个方法kaoQin();
b、有一个抽象类Student,实现接口Kaoqinable,属性:sid,sname c、Student有两个子类:AbsentStudent类在实现kaoQin()方法时输出学生姓名+“到!到!到!”;NonAbsentStudent类在实现kaoQin()方法时输出学生姓名+“交2000字检讨!!!”;
d、王书记属于Teacher类,属性:tid,tname;方法:chaKaoQin(Student s),要求用到多态;
e、该应用程序中有一个主类Test,在主类Test的main方法中进行测试。

interface Kaoqinable{
 public void kaoqin();
}
abstract class Student{
 int sid;
 String sname;
}
class AbsentStudent extends Student{
 public void kaoqin() {
  System.out.println(sname+"到!到!到!");
 }
}
class NonAbsentStudent extends Student{
 public void kaoqin() {
  System.out.println(sname+"交2000字检讨!");
 }
}
class Teacher {
 int tid;
 String tname;
 public void chaKaoQin(Student s) {
  System.out.println(s.sname+"在吗?");
 }
 public void chaKaoQin() {
  System.out.println("有人在吗?");
 }
}
public class Test3 {
 public static void main(String[] args) {
  AbsentStudent s1=new AbsentStudent();
      NonAbsentStudent s2=new NonAbsentStudent();
      s1.sname="小何";
      s2.sname="小王";
  Teacher t1=new Teacher();
      t1.tname="王书记";
      t1.chaKaoQin(s1);
      t1.chaKaoQin();
      s1.kaoqin();
      s2.kaoqin();
 }  
}

6、将“明明可以靠脸吃饭,偏偏要靠才华。中华儿女多奇志,不爱红装爱才智。 ”将这两句话写入到D盘下的my.txt文件中,注意两句话之间需要换行,要求进行异常处理。

import java.io.BufferedReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 public class Test4 {
     public static void main(String[] args) {
         FileWriter fw=null;
         try {
             fw = new FileWriter("d:\\my.txt");
             fw.write("明明可以靠脸吃饭,偏偏要靠才华。\n" +
                     "中华儿女多奇志,不爱红装爱才智。");
         } catch (IOException e) {
             e.printStackTrace();
         }finally {
             try {
                 fw.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 }

7、将字符串” 01机械学院,02 电气学院,03材化学院,04生食品学院,05土建学院,06计算机学院”通过 “,”进行分割并添加到集合中,然后遍历集合输出其元素。

import java.util.ArrayList;
import java.util.List;
public class Test5 {
 public static void main(String[] args) {
  String str="01机械学院,02 电气学院,03材化学院,04生食品学院,05土建学院,06计算机学院";
  List<String> list=new ArrayList<>();
  String[] strArray = str.split(",");
  for (String con :strArray) {
   list.add(con);
  }
  for(String obj:list){
   System.out.println(obj);
   }
  }
}

8、编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次。

class T1 extends Thread{
 String name=Thread.currentThread().getName();
 public void run() {
  for(int i=1;i<=20;i++) {
  System.out.println(name);
  try {
   Thread.sleep(300);
  }catch(InterruptedException e) {
   e.printStackTrace();
  }
 }
 }
}
public class Test6 {
 public static void main(String[] args) {
  T1 a=new T1();
  T1 b=new T1();
  a.name="线程1";
  b.name="线程2";
  a.start();
  b.start();
 }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章