How to Create a Java Code to Check Multiple Question

how to create a Java code to check multiple question

The number, which is a multiple of n, shall be zero when divided by n. And zero is divided by no matter what the value, the rest is zero. Therefore, if the number you received is not zero but divided by 2, 3, 4, and 10, program will print the number as multiple.

  import java.util.Scanner;


public class selection {
public static void main(String[] args) {
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter a number:");
n = s.nextInt();
if (n > 0) {
System.out.println("The number " + n + " is Positive");
} else if (n < 0) {
System.out.println("The number " + n + " is Negative");
} else {
System.out.println("The number " + n + " neither Positive nor Negative ");
}

if(n%2==0 && n!=0){
System.out.println("The number " + n + " is Multiplication of two");
}
if(n%3==0 && n!=0){
System.out.println("The number " + n + " is Multiplication of three");
}
if(n%4==0 && n!=0){
System.out.println("The number " + n + " is Multiplication of four");
}
if(n%10==0 && n!=0){
System.out.println("The number " + n + " is Multiplication of ten");
}

if(n%2!=0 && n%3!=0 && n%4 !=0 && n%10!=0 || n==0){
System.out.println("The number " + n + " is not a multiple of 2 and 3 and 4 and 10");
}
}


}

I am trying to build a Multiple choice test in java and for some reason my code will only work for two questions and not anymore

public class Main2 {

public class Questions {

String Question;
String userAns;
String realAns;
}

static int count = 0;
public static void main(String[] args) {
JFrame frame = new JFrame("Screen");
frame.setSize(2500, 2500);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
frame.getContentPane().add(panel);
panel.setSize(2500, 2500);
panel.setLayout(null);
//panel.setBackground(Color.red);

Question[] questions = new Question[3];
System.out.println(questions.length);
Question q1 = new Question();
q1.Question = "1) What is your Name?";
q1.A = "Bob";
q1.B = "Billy";
q1.C = "Joe";
q1.D = "Jill";
questions[0] = q1;

Question q2 = new Question();
q2.Question = "2) What is your Age?";
q2.A = "5";
q2.B = "69";
q2.C = "21";
q2.D = "12";
questions[1] = q2;

Question q3 = new Question();
q3.Question = "3) When Is your Birthday?";
q3.A = "May";
q3.B = "Jan";
q3.C = "Apr";
q3.D = "Aug";
questions[2] = q3;

//When this question is added the code breaks down
JLabel Question = new JLabel(questions[0].Question);
Question.setBounds(50, 0, 1500, 50);
panel.add(Question);
Question.setFont(new Font(Question.getFont().getName(), Font.PLAIN, 25));
Question.setVisible(true);

JLabel incorrectAnswerLabel = new JLabel("Incorrect Answer! Try Again");
incorrectAnswerLabel.setBounds(300, 0, 1000, 500);
panel.add(incorrectAnswerLabel);
incorrectAnswerLabel.setVisible(false);
incorrectAnswerLabel.setFont(new Font(incorrectAnswerLabel.getFont().getName(), Font.BOLD, 46));
incorrectAnswerLabel.setForeground(Color.RED);

JLabel correctAnswerLabel = new JLabel("Correct Answer! Good Job");
correctAnswerLabel.setBounds(300, 0, 1000, 500);
panel.add(correctAnswerLabel);
correctAnswerLabel.setVisible(false);
correctAnswerLabel.setFont(new Font(correctAnswerLabel.getFont().getName(), Font.BOLD, 46));
correctAnswerLabel.setForeground(Color.GREEN);

JButton submitButton = new JButton();
submitButton.setBounds(50, 250, 150, 50);
submitButton.setText("Submit");
panel.add(submitButton);
submitButton.setVisible(true);

JRadioButton OptionA = new JRadioButton(questions[0].A);
OptionA.setBounds(50, 50, 100, 50);
panel.add(OptionA);
OptionA.setVisible(true);

JRadioButton OptionB = new JRadioButton(questions[0].B);
OptionB.setBounds(50, 100, 100, 50);
panel.add(OptionB);
OptionB.setVisible(true);

JRadioButton OptionC = new JRadioButton(questions[0].C);
OptionC.setBounds(50, 150, 100, 50);
panel.add(OptionC);
OptionC.setVisible(true);

JRadioButton OptionD = new JRadioButton(questions[0].D);
OptionD.setBounds(50, 200, 100, 50);
panel.add(OptionD);
OptionD.setVisible(true);

ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(OptionA);
radioGroup.add(OptionB);
radioGroup.add(OptionC);
radioGroup.add(OptionD);

submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (submitButton.getText().equals("Submit")) {

if (OptionA.isSelected()) {
submitButton.setText("Next Question");
correctAnswerLabel.setVisible(true);

} else {
incorrectAnswerLabel.setVisible(true);
}
} else {
correctAnswerLabel.setVisible(false);
submitButton.setText("Submit");

if(count < questions.length-1){
count = count+1;
}
Question q = questions[count];
Question.setText(q.Question);
setAnswers(q.A, q.B, q.C, q.D, OptionA, OptionB, OptionC, OptionD);
}
}

});

}

static void setAnswers(String A, String B, String C, String D, JRadioButton a, JRadioButton b, JRadioButton c, JRadioButton d) {
List<String> answers = Arrays.asList(A, B, C, D);
Collections.shuffle(answers);
a.setText(answers.get(0));
b.setText(answers.get(1));
c.setText(answers.get(2));
d.setText(answers.get(3));
}

}

Can you add multiple questions in a while loop in java?

Assuming that you have prepared a 2D array of questions and answers:

final Scanner s = new Scanner(System.in);

String[][] arrQA = {
{"question1", "answer1"},
{"question2", "answer2"},
{"question3", "answer3"},
{"question4", "answer4"},
};
int lives = 3;
int success = 0;

for (int i = 0; i < arrQA.length && lives > 0; i++) {
String[] qa = arrQA[i];

System.out.print(qa[0] + " goes here, type your answer: ");
String answer = s.nextLine();

if (answer.equalsIgnoreCase(qa[1])) {
success++;
System.out.println("Correct! Please press ENTER to continue");
s.nextLine();
} else {
--lives;
System.out.println("Incorrect! You have " + lives + " lives left");
}
}
System.out.println("Game Over! You have answered " + success + " questions correctly");

Using if/ else statement to check multiple answers?

First of all = + is not correct, you should use += to add the value to TotalCorrect, second adding 0 does nothing, so why do it at all?

So why not just do:

if (ConvertedAnswer1 == Answer1){ TotalCorrect += 1; }
if (ConvertedAnswer2 == Answer2){ TotalCorrect += 1; }
if (ConvertedAnswer3 == Answer3){ TotalCorrect += 1; }
if (ConvertedAnswer4 == Answer4){ TotalCorrect += 1; }

You could probably do it even prettier, but this will work.

Program that asks a multiple-choice question won't show the user an answer after an invalid character is entered

It's best to use a loop here. You keep looping until the user enters a valid response:

askQuestion(); 

while (true) {

userChoice = getAnswer();

switch(userChoice)
{
case 'a':
case 'A':
System.out.println("Incorrect! 'Switch' IS a key word. The correct answer was B. ");
System.out.println("The program will now end. Thanks for answering!");
System.exit(0);

...

default:
System.out.println("Invalid character.");
}
}

Note that you don't need the break statements after each System.exit(0) call.

You could move the askQuestion() call inside the while loop if you wanted to re-ask the question on a bad input.

Java: Which structure to use for a multiple choice game

Java is an Object-Oriented language. You should use it.

Example: Create a class representing a question and the possible answers.

public class Question {
private String question;
private String correctAnswer;
private List<String> answerList;
}

Now, you can create a good useful constructor, where you give the question first, then all the answers using varargs, with the correct answer first:

new Question("How satisfied are you with this answer?",
"Extremely satisfied",
"Very satisfied",
"Somewhat satisfied",
"Not so satisfied");

Your constructor could then build the answers list and call Collections.shuffle(this.answers) to randomize them.

You then add all the questions to a List<Question> and call shuffle on that, so questions will be asked in random order, and only once.



Related Topics



Leave a reply



Submit