Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Programming Assignment Unit 1

The document contains code for a Java quiz game program that asks 5 multiple choice questions about Zambia and calculates the user's score. It imports necessary libraries, defines a main method to run the quiz using a Scanner, asks questions with answer options, checks the user's answers, and prints the final score.

Uploaded by

albertmutoya57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Programming Assignment Unit 1

The document contains code for a Java quiz game program that asks 5 multiple choice questions about Zambia and calculates the user's score. It imports necessary libraries, defines a main method to run the quiz using a Scanner, asks questions with answer options, checks the user's answers, and prints the final score.

Uploaded by

albertmutoya57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT-1 QUIZ GAME

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Your main method code here
int correctAnswers = 0;
int totalQuestions = 5;

// Create a Scanner object to read user input


Scanner scanner = new Scanner(System.in);

// Display a welcome message


System.out.println("Welcome to the Quiz Game!");

// Question 1
System.out.println("1. What is the capital of Zambia?");
System.out.println("A. Berlin");
System.out.println("B. Lusaka");
System.out.println("C. London");
System.out.println("D. Rome");
char answer1 = getUserAnswer(scanner);
if (answer1 == 'B') {
correctAnswers++;
}

// Question 2
System.out.println("2. Which planet is the biggest?");
System.out.println("A. Venus");
System.out.println("B. Mars");
System.out.println("C. Jupiter");
System.out.println("D. Saturn");
char answer2 = getUserAnswer(scanner);
if (answer2 == 'C') {
correctAnswers++;
}

// Add three more questions following the same pattern...

// Question 3
System.out.println("3. Victoria Falls is located on which river?");
System.out.println("A. Kafue River");
System.out.println("B. Zambezi River");
System.out.println("C. Luangwa River");
System.out.println("D. Luapula River");
char answer3 = getUserAnswer(scanner);
if (answer3 == 'B') {
correctAnswers++;
}

// Question 4
System.out.println("4. The Luapola River marks the border between Zambia and which
country?");
System.out.println("A. Democratic Republic of the Congo");
System.out.println("B. Namibia");
System.out.println("C. Angola");
System.out.println("D. Botswana");
char answer4 = getUserAnswer(scanner);
if (answer4 == 'A') {
correctAnswers++;
}

// Question 5
System.out.println("5. In 1935, Lusaka replaced which city that was the capital of
Northern Rhodesia?");
System.out.println("A. Rhodes");
System.out.println("B. Stanley");
System.out.println("C. Salisbury");
System.out.println("D. Livingstone");
char answer5 = getUserAnswer(scanner);
if (answer5 == 'D') {
correctAnswers++;
}
// Display the final score
double score = ((double) correctAnswers / totalQuestions) * 100;
System.out.println("\nYour Final Score: " + score + "%");

// Close the Scanner


scanner.close();
}

// Helper method to get user's answer


private static char getUserAnswer(Scanner scanner) {
System.out.print("Your answer (A/B/C/D): ");
char userAnswer = Character.toUpperCase(scanner.next().charAt(0));
while (userAnswer < 'A' || userAnswer > 'D') {
System.out.println("Invalid input. Please enter A, B, C, or D.");
System.out.print("Your answer (A/B/C/D): ");
userAnswer = Character.toUpperCase(scanner.next().charAt(0));
}
return userAnswer;
}
}

You might also like