编程使用缓冲流读取试题文件,test6_5.txt 内容如下所示。 每次显示试题文件中的一道题目,读取到字符“*”时暂停读取, 等待用户从键盘输入答案。用户做完全部题目后,程序给出用户的得分。
test6_5.txt内容如下:
(1)面向对象程序设计中,把对象的属性和行为组织在同一个模块内的机制叫做( )。
A.封装象 B.继承 C.抽象 D.多态
********************
(2)在面向对象程序设计中,类通过( )与外界发生关系。
A.对象 B.类 C.消息 D.接口
********************
(3)类的析构函数是( )时调用的。
A.类创建 B.创建对象 C.引用对象 D.释放对象
********************
(4)在下面的关键字中, ( )能声明类成员是私有的。
A.protected B.const C.friend D.private
********************
题目难点分析:
1.如何控制在读取到字符"*"暂停读取,我在这里还没有完全实现,但是不影响题目本意
2.如何控制用户输入答案后,再读取下一题
3.判断题目正确与否并计分.
1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.FileReader; 4 import java.io.IOException; 5 import java.util.Scanner; 6 7 public class Test6_5 { 8 public static void main(String[] args) throws IOException{ 9 10 Scanner input=new Scanner(System.in); 11 File file = new File("d:\Test6_5.txt"); 12 FileReader fr = new FileReader(file); 13 BufferedReader br = new BufferedReader(fr); 14 15 String s;int count=0;String c;int score=0; 16 17 do{ 18 while((s=br.readLine())!=null){ 19 System.out.println(s); 20 if(s.startsWith("*")) break;//用string类的startsWith方法,是否以"*"开头,返回值是boolean型. 21 } 22 System.out.println("请输入答案:"); 23 c=input.next(); 24 ++count;//控制题目显示 25 score+=Judge(count,c);//判断题目正确与否并计分 26 27 }while(count!=4); 28 System.out.println("分数:"+score); 29 fr.close(); 30 } 31 32 public static int Judge(int count,String c){ 33 int score=0; 34 if(count==1&&c.equals("A")) score=25; 35 if(count==2&&c.equals("C")) score=25; 36 if(count==3&&c.equals("B")) score=25; 37 if(count==4&&c.equals("D")) score=25; 38 return score; 39 } 40 } 41