使用缓冲流读取试题文件,每次显示试题文件中的一道题目。读取到字符“*”时候暂停读取,等待用户从键盘输入答案。用户做完全部题目后,程序给出用户的得分。Test.txt如下:

xiaoxiao2021-02-27  355

題目內容:

使用缓冲流读取试题文件,每次显示试题文件中的一道题目。读取到字符“*”时候暂停读取,等待用户从键盘输入答案。用户做完全部题目后,程序给出用户的得分。Test.txt如下:

 

(1)北京奥运是什么时间开幕的?    A.2008-08-08 B. 2008-08-01    C.2008-10-01D. 2008-07-08 ******************** (2)下列哪个国家不属于亚洲?    A.沙特  B.印度C.巴西  D.越南 ******************** (3)下列哪个国家最爱足球?    A.刚果  B.越南C.老挝  D.巴西 ******************** (4)下列哪种动物属于猫科动物?    A.鬣狗  B.犀牛C.大象 D.狮子 ********************

 

输入输出说明: a import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan =new Scanner(System.in); File file = new File("a.txt"); String s,ans = "ABCD"; int score = 0; StringBuffer result = new StringBuffer(); try { FileReader r = new FileReader(file); BufferedReader br = new BufferedReader(r); while((s=br.readLine())!=null){ if(!s.startsWith("*")){ System.out.println(s); }else{ System.out.println("Please input your answer"); result.append(scan.next().charAt(0)); } } br.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(int i=0;i<result.length();i++){ if(result.charAt(i)==ans.charAt(i)) score += 25; } System.out.println("Final score is "+score); } }
转载请注明原文地址: https://www.6miu.com/read-4771.html

最新回复(0)