产生个1-10的随机数 i   当i= 5时  就从文件中读出第5题 当 i=10 时 从文件中读出第十题
import java.io.*;
public class Testa {
public static void main(String args[]) {
try {
 BufferedReader in = new BufferedReader(new FileReader("驾驶理论考试.txt"));
  String t;
  int index ,w=0 ;
  t = in.readLine();
  int num = (int)(Math.random()*10) +1;
  System.out.println(num);
  index = t.indexOf("num");
  System.out.println(index);
  while(t.length() == 0 || index != 0) {
    if(t.length()!=0) {
      t = in.readLine();
      index = t.indexOf("num");
    }
  }
  
  while(t.length()!=0) {
   System.out.println(t);
   t = in.readLine();
  }
  }
    
catch(FileNotFoundException e){}
catch(IOException e){}

}
}驾驶理论考试.txt
内容是
1、  行车中遇有浓雾或特大雾天,能见度过低行车困难时,应_____。 
A、开启前照灯行驶
B、开启示廓灯、雾灯行驶
C、选择安全地点停车
D、开启危险报警闪光灯行驶   2、  驾驶人驾驶机动车上道路行驶前,应当对机动车的_____进行认真检查。 
A、安全技术状况
B、整体结构
C、所有部件
D、齿轮油   3、  行车中发动机突然熄火后,应_____。 
A、立即减速停车
B、关闭点火开关
C、将变速器操纵杆置于空挡行驶
D、开启右转向灯,将车缓慢滑行到路边停车检查   4、  公安机关交通管理部门对非本辖区机动车有违法行为记录的,可以将记录违法行为的信息、证据转至_____公安机关交通管理部门。 
A、机动车号牌核发地
B、驾驶证核发地
C、资格证核发地
D、身份证核发地   5、  驾驶人进入驾驶室前,首先应_____并确认安全。 
A、观察车辆周围情况
B、不用观察周围情况
C、开启车门直接上车
D、注意观察天气情况   6、  当事人逾期不履行行政处罚决定的,作出行政处罚决定的行政机关可以_____。 
A、申请人民法院强制执行
B、申请人民检察院强制执行
C、吊销其机动车驾驶证
D、处15日以下拘留   
 
7、  夜间行车中,如果灯光照射_____,有可能是车辆前方出现急转弯或大坑。 
A、由远及近
B、离开路面
C、距离不变
D、由高变低   8、  机动车驶近急弯等影响安全视距的路段时,应当 _____ ,并鸣喇叭示意。 
A、加速通过
B、减速慢行
C、使用危险报警闪光灯
D、随意通行   9、  下长坡时,车速会因为惯性而越来越快,控制车速最有效的方法是_____。 
A、挂入空挡滑行
B、利用发动机制动
C、踏下离合器滑行
D、用行车制动控制车速   10、  车辆在高速公路意外碰撞护栏时,有效的保护措施是_____。 
A、握紧转向盘,适当修正
B、向相反方向大幅度转向
C、迅速向碰撞一侧转向
D、迅速采取紧急制动 

解决方案 »

  1.   

    随机数没问题的,(int)9.99结果是9所以最多是10。
    楼主的循环我没看明白,而且运行起来超慢,不知道怎么回事,我重新改了下
    有点困了,可能代码写的比较随意,参考下吧。import java.io.*;
    import java.util.Random;public class Testa {
    public static void main(String args[]) {
    try {
    String t;
    int num = (int) (Math.random() * 10) + 1;
    System.out.println(num);
    BufferedReader in = new BufferedReader(new FileReader(
    "f:/驾驶理论考试.txt")); while (true) {
    String thisLine = in.readLine();
    if (thisLine == null) {
    break;
    } else if (thisLine.startsWith(num + "、")) {
    System.out.println(thisLine);
    }
    }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
    }
    }
      

  2.   

    写完了,发现看错题了。不过功能类似吧,根据控制台输入的数字选出题目。
    import java.io.*;
    import java.util.*;public class FindTopic {
    private static File exam = new File("C:/exam.txt");
    private static int topicNum;

    public static void printTopic(int i){
    try{
    BufferedReader bf = new BufferedReader(new FileReader(exam));
    StringBuffer sb = new StringBuffer(); 
    String text;

    while ((text = bf.readLine()) != null){
    if (text.startsWith(i +"、")){
    sb.append(text +"\n");

    while ((text = bf.readLine()) != null){
    if (text.matches("\\d*|\\s*\\t*\n"))
    break;
    sb.append(text +"\n");
    }
    }
    }
    if (sb.toString().equals(""))
    System.err.println("No Such Topic!");
    else
    System.out.println(sb.toString());

    }catch(FileNotFoundException e){
    System.err.println("File Not Found!");
    }catch(IOException e){
    System.err.println("IO Excepton!");
    }
    }

    public static void main(String args[]){
    Scanner scan = new Scanner(System.in);
    System.out.println("Please select a topic: ");

    while ((topicNum = scan.nextInt()) != -1){
    System.out.println("The topic you selected is: ");
    printTopic(topicNum);

    System.out.println("Please select a topic: ");
    }

    System.out.println("Exit!");
    }
    }
      

  3.   

    看了个片子回来发现前面代码是有点问题,答案没打出来,现在应该可以了。
    import java.io.*;
    import java.util.Random;public class Testa {
    public static void main(String args[]) {
    String t;
    String thisLine;
    String question = "";
    int num = (int) (Math.random() * 10) + 1;
    System.out.println("抽中第" + num + "题:");
    BufferedReader in;
    try {
    in = new BufferedReader(new FileReader("f:/驾驶理论考试.txt"));
    while (true) {
    thisLine = in.readLine();
    if (thisLine == null) {
    break;
    } else if (thisLine.startsWith(num + "、")) {
    question = thisLine + "\n";
    while (true) {
    thisLine = in.readLine();
    if (thisLine == null || thisLine.length() < 2) {
    break;
    }
    question += thisLine + "\n";
    }
    }
    }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
    System.out.println(question);
    }
    }