01/10/2018, 15:04

Dùng lệnh "if" trong java

Em mới là sinh viên năm nhất mới tập tọe dùng JAVA GUI. Em đang vướng lỗi này các tiền bối giúp em với ạ.

 hardButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                callquestions = callquestions + 1;

                if (callquestions == 1);{
                            questionBox.setText("How many players are there in an ice hockey team?");
                            answerA.setText("5");
                            answerB.setText("6");
                            answerC.setText("7");
                            answerD.setText("8");}

                if (callquestions == 2) ;{

                            questionBox.setText("What is Zumba?");
                            answerA.setText("A city in Spain");
                            answerB.setText("A type of Underwater Sports");
                            answerC.setText("Name of a famous boxer");
                            answerD.setText("A Latin inspired dance-based workout");}

                if (callquestions == 3) ;{

                            questionBox.setText("What is number of teams participate in Premier League?");
                            answerA.setText("18");
                            answerB.setText("19");
                            answerC.setText("20");
                            answerD.setText("21");}

                if (callquestions == 4) ;{

                            questionBox.setText("Where is the 2018 winter olympic games going to be held?");
                            answerA.setText("Beijing");
                            answerB.setText("PyeongChang");
                            answerC.setText("Hanoi");
                            answerD.setText("Moscow");}

                if (callquestions == 5) ;{

                            questionBox.setText("The FIFA World Cup championship has been awarded every … years?");
                            answerA.setText("2");
                            answerB.setText("3");
                            answerC.setText("4");
                            answerD.setText("5");
 }

Code của em nó như thế này ạ bên ngoài ActionListener em đã
public int callquestions;{
callquestions = 0 } rồi

nhưng mà khi Run thì bấm nút nó chỉ ra đúng câu cuối mà không chạy các câu trước, nhờ các tiền bối giúp em với ạ

Tao Không Ngu. viết 17:11 ngày 01/10/2018

Hi Phan Anh.

  1. Trước khi code UI bạn nên học Java core trước.
  2. Xem lại if.
Lê Bảo Châu viết 17:08 ngày 01/10/2018

if (callquestions == 1);{

Đọc đến đây biết kiến thức cơ bản còn chưa nắm rõ rồi

Nguyễn Đình Anh viết 17:05 ngày 01/10/2018

Học lại về Core đi bạn ơi, hàm if sai tùm lum kìa

Cong 5 viết 17:05 ngày 01/10/2018

trước khi học java bạn đã học ngôn ngữ gì rồi sao viết if lạ thế …

Giang Phan viết 17:10 ngày 01/10/2018

Xem lại Java core nha bạn. Bài viết đơn giản, dễ hiểu nhất:

GP Coder – 26 Oct 17

Mệnh đề if-else trong java - GP Coder (Lập trình Java)

Hướng dẫn lập trình Java, Design Pattern, Spring, JSF, Primefaces, Collection, Multi-thread, Webservice, SQL, Report, Apache POI.

Trần Đức Vương viết 17:09 ngày 01/10/2018

Bỏ dấu chấm phẩy đi… sao hôm nọ cũng có người đăng code có chấm phẩy lên stackoverflow nhỉ =D
if (callquestions == 1);{...}
thay bằng
if (callquestions == 1){...}

Bài liên quan
0