Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class labfivepartone {
- public static void main(String args[]) {
- int red = 1, green = 2, blue = 3, yellow = 4, orange = 5, purple = 6;
- Scanner userInput = new Scanner(System.in);
- boolean running = true;
- int counter = 1;
- double correct = 0, incorrect = 0;
- while(running) {
- int selection = randomNum(5) + 1;
- System.out.println("Guess what color the computer picked:\n"
- + "1.Red\n2.Green\n3.Blue\n4.Yellow\n5.Orange\n6.Purple");
- int userSelection = userInput.nextInt();
- if(userSelection == selection) {
- System.out.println("You guessed correctly !!!");
- correct++;
- } else {
- System.out.println("You guessed incorrectly !!!");
- incorrect++;
- }
- if(counter == 10) {
- double average = averageResult(correct, incorrect);
- System.out.printf("Correct Percentage: %.2f%%", average);
- System.exit(0);
- }
- counter++;
- }
- userInput.close();
- }
- public static int randomNum(int range) {
- Random rand = new Random();
- int result = rand.nextInt(6) + 1;
- return result;
- }
- public static double averageResult(double correct, double incorrect) {
- double result = (correct / incorrect) * 100;
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement