This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
130 lines (108 loc) · 4.39 KB
/
Main.java
File metadata and controls
130 lines (108 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Main extends JFrame implements ActionListener {
// UI components
JLabel questionLabel;
JRadioButton option1, option2, option3, option4;
ButtonGroup optionsGroup;
JButton nextButton;
JLabel resultLabel;
// Questions and answers
String[] questions = {
"What is the capital city of India?",
"What is the currency of India?",
"What is the official language of the Indian state of Tamil Nadu?",
"Which festival is known as the Festival of Lights in India?",
"Who is known as the Iron Man of India?",
"What is the national animal of India?"
};
String[][] optionsData = {
{"Bengaluru", "Mumbai", "New Delhi", "Kolkata"},
{"Euro", "Indian Rupee", "Peso", "Yen"},
{"Kannada", "Tamil", "Hindi", "Bengali"},
{"Diwali", "Holi", "Eid", "Navratri"},
{"Sardar Vallabhbhai Patel", "Bhagat Singh", "Mahatma Gandhi", "Jawaharlal Nehru"},
{"Elephant", "Bengal Tiger", "Lion", "Peacock"}
};
int[] correctAnswers = {3, 2, 2, 1, 1, 2}; // Correct answers (1-based index)
int currentQuestionIndex = 0;
int score = 0;
// Constructor to set up the UI and display the first question
public Main() {
// Frame settings
setTitle("Quiz Application");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(6, 1));
// Create question label and option buttons
questionLabel = new JLabel("Question", JLabel.CENTER);
option1 = new JRadioButton();
option2 = new JRadioButton();
option3 = new JRadioButton();
option4 = new JRadioButton();
optionsGroup = new ButtonGroup();
optionsGroup.add(option1);
optionsGroup.add(option2);
optionsGroup.add(option3);
optionsGroup.add(option4);
nextButton = new JButton("Next");
nextButton.addActionListener(this);
resultLabel = new JLabel("", JLabel.CENTER);
// Add components to the frame
add(questionLabel);
add(option1);
add(option2);
add(option3);
add(option4);
add(nextButton);
add(resultLabel);
// Load the first question
loadQuestion();
setVisible(true);
}
// Load the current question and options
public void loadQuestion() {
questionLabel.setText(questions[currentQuestionIndex]);
option1.setText(optionsData[currentQuestionIndex][0]);
option2.setText(optionsData[currentQuestionIndex][1]);
option3.setText(optionsData[currentQuestionIndex][2]);
option4.setText(optionsData[currentQuestionIndex][3]);
resultLabel.setText("");
optionsGroup.clearSelection(); // Clear any previous selections
}
// Handle button click for "Next"
@Override
public void actionPerformed(ActionEvent e) {
// Check which option is selected
int selectedAnswer = -1;
if (option1.isSelected()) selectedAnswer = 1;
if (option2.isSelected()) selectedAnswer = 2;
if (option3.isSelected()) selectedAnswer = 3;
if (option4.isSelected()) selectedAnswer = 4;
// If no option is selected, show a message
if (selectedAnswer == -1) {
resultLabel.setText("Please select an option!");
return;
}
// Check if the answer is correct
if (selectedAnswer == correctAnswers[currentQuestionIndex]) {
score++;
resultLabel.setText("Correct!");
} else {
resultLabel.setText("Wrong! Correct answer: " + optionsData[currentQuestionIndex][correctAnswers[currentQuestionIndex] - 1]);
}
currentQuestionIndex++;
// Load the next question or show the final score
if (currentQuestionIndex < questions.length) {
loadQuestion();
} else {
JOptionPane.showMessageDialog(this, "Quiz Over! Your score: " + score + "/" + questions.length);
System.exit(0); // Close the application
}
}
public static void main(String[] args) {
new Main();
}
}