Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class EvenOddQuiz {
- public static void main(String args[]) {
- int rights = 0;
- int wrongs = 0;
- //create Scanner for user input
- Scanner scanner = new Scanner(System.in);
- for (int i = 1; i <=7; i++) {
- //generate a number between 1 and 20 and cast it to int
- int randInt = (int) (1 + Math.random() * 20);
- //show this randInt to the user
- System.out.print("Is " + randInt + " odd or even?");
- //get the response from the user
- String userAnswer = scanner.next();
- //set the correct answer so even or odd
- String correctAnswer;
- if (randInt % 2 == 0) {
- correctAnswer = "even";
- }
- else {
- correctAnswer = "odd";
- }
- //compare the user response to correct answer
- if (userAnswer.equals(correctAnswer)) {
- rights++;
- }
- else {
- wrongs++;
- }
- }
- System.out.println("rights = " + rights);
- System.out.println("wrongs = " + wrongs);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment