Java—暂停for循环并在用户按下按钮时恢复迭代

Java—暂停for循环并在用户按下按钮时恢复迭代,java,button,input,for-loop,Java,Button,Input,For Loop,我正在用Java中的SWT做一个测验。我有一个包含问题和答案的课程。在主类中,当用户通过对话框输入其姓名和问题数量并按OK时,测验开始。 我有一个for循环,它应该迭代每个问题,例如,第一次迭代应该给用户一个问题,它应该等到用户通过文本框输入答案。当用户按下“下一步”按钮时,如果答案正确,他将收到一条消息,并进入第二次迭代,依此类推。 我的问题是,每次他开始测验时,for循环都不会等待用户输入和按键操作,而是直接进入最后一个问题(for循环的最后一次迭代) 有人能给我举个例子吗?我该怎么做?您需

我正在用Java中的SWT做一个测验。我有一个包含问题和答案的课程。在主类中,当用户通过对话框输入其姓名和问题数量并按OK时,测验开始。 我有一个for循环,它应该迭代每个问题,例如,第一次迭代应该给用户一个问题,它应该等到用户通过文本框输入答案。当用户按下“下一步”按钮时,如果答案正确,他将收到一条消息,并进入第二次迭代,依此类推。 我的问题是,每次他开始测验时,for循环都不会等待用户输入和按键操作,而是直接进入最后一个问题(for循环的最后一次迭代)


有人能给我举个例子吗?我该怎么做?

您需要根据事件更改问题编号。以下是伪代码:

private int count = 0;

startMethod()
{
   askQuestion(0);
}

onTextInput()
{
    checkAnswer();
    count++;
    askQuestion(count);
}

这是完整的代码。希望你会觉得它有用

import org.eclipse.core.internal.databinding.Pair;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Quiz {

    protected Shell shell;
    private Text text;
    private Pair[] questions ;
    private int number_of_question = 10;
    private int current_question = 0;
    public static void main(String[] args) {
        try {
            Quiz window = new Quiz();
            window.open();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while(!shell.isDisposed()) {
            if(!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    protected void createContents() {       
        createQuiz();   

        shell = new Shell();
        shell.setSize(450,300);
        shell.setText("SWT Application");

        final Label lblTheQuestion = new Label(shell, SWT.NONE);
        lblTheQuestion.setBounds(45, 38, 124, 15);
        lblTheQuestion.setText((String) questions[current_question].a);

        text = new Text(shell, SWT.BORDER);
        text.setBounds(45, 88, 76, 21);

        Button btnNext = new Button(shell, SWT.NONE);
        btnNext.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {

                if(text.getText().equals((String) questions[current_question].b)) {
                    new Thread(new Runnable() {
                        public void run() {                                 
                                shell.getDisplay().syncExec(new Runnable() {                                
                                    @Override
                                    public void run() {
                                        current_question++;
                                        lblTheQuestion.setText((String) questions[current_question].a);
                                        lblTheQuestion.redraw();    
                                    }
                                }); 
                        }

                    }).start();
                }       
            }
        });
        btnNext.setBounds(188, 55, 75, 25);
        btnNext.setText("Next");
    }
    private void createQuiz() { 
        questions = new Pair[number_of_question];
        for(int i = 0; i<number_of_question; i++) {
            questions[i] = new Pair("Question"+i,""+i);
        }

    }

}
import org.eclipse.core.internal.databinding.Pair;
导入org.eclipse.swt.widgets.Display;
导入org.eclipse.swt.widgets.Shell;
导入org.eclipse.swt.widgets.Label;
导入org.eclipse.swt.swt;
导入org.eclipse.swt.widgets.Text;
导入org.eclipse.swt.widgets.Button;
导入org.eclipse.swt.events.SelectionAdapter;
导入org.eclipse.swt.events.SelectionEvent;
公开课测验{
保护壳;
私人文本;
私人问题;
问题的私有整数=10;
私人int当前_问题=0;
公共静态void main(字符串[]args){
试一试{
测验窗口=新测验();
window.open();
}捕获(例外e){
e、 printStackTrace();
}
}
公开作废{
Display=Display.getDefault();
createContents();
shell.open();
shell.layout();
而(!shell.isDisposed()){
如果(!display.readAndDispatch()){
display.sleep();
}
}
}
受保护的void createContents(){
createquick();
外壳=新外壳();
外壳尺寸(450300);
shell.setText(“SWT应用程序”);
最终标签lblTheQuestion=新标签(外壳,SWT.NONE);
lblTheQuestion.挫折(45,38,124,15);
lblthquestion.setText((字符串)问题[当前问题].a);
text=新文本(shell、SWT.BORDER);
文本.背景(45,88,76,21);
按钮btnNext=新按钮(外壳,SWT.NONE);
btnNext.addSelectionListener(新的SelectionAdapter(){
@凌驾
公共无效WidgeSelected(SelectionEvent e){
if(text.getText().equals((字符串)问题[current_question].b)){
新线程(newrunnable()){
public void run(){
shell.getDisplay().syncExec(新的Runnable(){
@凌驾
公开募捐{
当前_问题++;
lblthquestion.setText((字符串)问题[当前问题].a);
lblTheQuestion.redraw();
}
}); 
}
}).start();
}       
}
});
b下一个立根(188,55,75,25);
btnNext.setText(“下一个”);
}
私有void createquick(){
问题=新的一对[问题的数量];
对于(int i=0;i