我用java编了一个单项选择题小程序(原程序是单项选择题,被我改动后和数据库连接),但是当我点击<确定>按钮后,数据库中的试题结果和我选择的结果完全相同,但就是显示‘错’,请高手们帮我看看,下面是数据库脚本和源代码:数据库脚本:create database nitin
use nitin
create table timu(
number int identity(1 , 1),
question varchar(500) not null,
optionA varchar(100) not null,
optionB varchar(100) not null,
optionC varchar(100) not null,
optionD varchar(100) not null,
answer char(2) not null,
)insert into timu values('Which is a method of the MouseMotionListener interface?','Public void mouseMoved(MouseEvent)','Public boolean mouseMoved(MouseEvent)','Public void mouseMoved(MouseMotionEvent)','Public boolean MouseMoved(MouseMotionEvent)','A')
insert into timu values('Which can NOT be used in declaring or declaring and initializing an automatic (method local) variable?','final','static','expressions','Constants of non-primitive type','B')
insert into timu values('Which can be used to encode charS for output?','Java.io.OutputStream.','Java.io.OutputStreamWriter.','Java.io.EncodeOutputStream.','Java.io.EncodeWriter.','B')
select * from timu源代码:import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.sql.*;public class FuXuan extends Applet implements ActionListener {
    static int i=0;
    static Connection con = null;
    static Statement stmt;
    static ResultSet rs = null;
    static String question[]=new String[3];//试题问题
    String ch[] = {"A", "B", "C", "D"};//试题选项
    static String answer[] = new String[3];//两道试题的答案
    Checkbox cb[] = new Checkbox[4];//与试题选项对应的4个复选框
    CheckboxGroup cbg = new CheckboxGroup();
    Label hint;//对错提示标签
    static TextArea content;//显示试题的内容的文本域
    Button ok;//确定选择的按钮
    static int bh = 0;//当前试题编号
    Button next;//下一道试题
    Button previous;//上一道试题
    public void init() {        
        setBackground(Color.yellow);
        setLayout(new BorderLayout());
        content = new TextArea("",10, 50,TextArea.SCROLLBARS_VERTICAL_ONLY);
        getQus();
         Font f=new Font("n1",Font.BOLD,30);         
        content.setFont(f);
        //content.disable();
        add("Center", content);        
        content.setText(question[bh]);
        Panel p = new Panel();
        p.setLayout(new GridLayout(2, 1));      
        Panel p1 = new Panel();//部署解答选项的面板
        for (int i = 0; i < ch.length; i++) {
            cb[i] = new Checkbox(ch[i],cbg,false);            
            p1.add(cb[i]);
        }
        p.add(p1);
        Panel p2 = new Panel();//部署确认按钮、对错提示、翻动试题按钮
        ok = new Button("确定");
        p2.add(ok);
        hint = new Label("对错提示");
        p2.add(hint);
        next = new Button("下一题");
        p2.add(next);
        previous = new Button("上一题");
        p2.add(previous);
        p.add(p2);
        add("South", p);
        next.addActionListener(this);
        previous.addActionListener(this);
        ok.addActionListener(this);
        resize(600,400);
    }
    //连接数据库取题和正确答案
    static void getQus() {
        try {
            String str = "SELECT * FROM timu ";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            /*Establish a connection with a data source*/
            con = DriverManager.getConnection("jdbc:sqlserver://20100410-1928:1234;databaseName=nitin;user=sa;password=1234");
            /*Create a Statement object to process the SELECT statement*/
            stmt = con.createStatement();
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = stmt.executeQuery(str);
            ResultSetMetaData rsmd=rs.getMetaData();
            while(rs.next()){                
                question[i]=rs.getString(1)+"."+rs.getString(2)+"\nA."+rs.getString(3)+"\nB."+rs.getString(4)+"\nC."+rs.getString(5)+"\nD."+rs.getString(6);
                System.out.println(question[i]);
                answer[i]=rs.getString(7).toString();
                System.out.println(answer[i]);
                i++;
                System.out.println(i);
            }          
        } catch (Exception e) { 
            
            System.out.println("Error occurred");
            System.out.println("Error:" + e);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
               if (stmt!= null) {
                try {
                    stmt.close();
                } catch (SQLException e2) {
                    e2.printStackTrace();
                }
            }
               if (con != null) {
                try {
                    con.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
        }
    }
    /*处理试题的翻动和点击“确定”的解答对错提示*/    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == ok) {//点击确认按钮,检查试题解答对错
            String s = "";          
            for (int i = 0; i < ch.length; i++) {
                if (cb[i].getState()) 
                    s = s+cb[i].getLabel();
                    //System.out.println(s);
                
            }
            if (s.equals(answer[bh])) {
                hint.setText("对");
            } else {
                
               
                System.out.println(s);
                System.out.println(answer[bh]);
               
                hint.setText("错");
            }
        } else if (e.getSource() == next) {//查看下一道试题
            
            if (bh <question.length-1) {                
                bh++;
            }
            content.setText(question[bh]);
        } else//查看上一道试题
        if (bh > 0) {
            bh--;
        }
        content.setText(question[bh]);
    }
}

解决方案 »

  1.   

     if (s.equals(answer[bh])) {
      hint.setText("对");
      } else {
        
        
      System.out.println(s);
      System.out.println(answer[bh]);
    ========================s.trim().equals(answer[bh].trim())) 
    lz  看看string 前后是不是有空格?????
    System.out.println(s.length());
      System.out.println(answer[bh].length); 看看相同么??????????????
      

  2.   

    学会使用debug,一步一步调,肯定能看到问题出在哪