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();
 }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章