当用户点击正确的答案时,它将转到下一个问题

时间:2016-01-19 23:11:13

标签: java shapes mouselistener

我设计了一个程序,每次点击一个形状时,它都会以有序的方式改变颜色。如果你看下面我的代码,我不知道在if语句中放什么。我想将一个值(如果可能的话,int值)返回到我的main方法,因此它知道继续下一个问题。我会接受任何建议,谢谢

public void mousePressed(MouseEvent e) { //when user clicks a button it goes in here

    k++; //increases k, every time it runs
    if (k == 4) //if k equal 4, it will go back to 0. Repeats the cycle
        k = 0;

    Point p = e.getPoint(); //get the postion of the click
    Polygon[] polys = polygonPanel.polygons;

    for (j = 0; j < polys.length; j++) { //check which polygon is picked
        if (polys[j].contains(p)) { //if the user clicks a shape it will go in this loop to change color    
            polygonPanel.colors[j] = getColor(k); //bring the four colors from the method
            polygonPanel.repaint(); //change the color of the polygon when clicked
            /*if (rightAnswer) {
                correctAnswer++;
            }*/
        }
    }
}

1 个答案:

答案 0 :(得分:0)

创建一个处理正确答案的函数,并在correctAnswer++行之后插入宏。

例如,我们可以这样做:

public void handleAnswer (int correctAnswer) {
    moveToNextQuestion(correctAnswer); //Takes the correct answer and moves to the question with the value "correctAnswer"
}

然后,在moveToNextQuestion函数中,您将添加切换问题的代码。