Advertisement
Guest User

guessinggame

a guest
Mar 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class labfivepartone {
  4.  
  5.     public static void main(String args[]) {
  6.        
  7.         int red = 1, green = 2, blue = 3, yellow = 4, orange = 5, purple = 6;
  8.         Scanner userInput = new Scanner(System.in);
  9.         boolean running = true;
  10.         int counter = 1;
  11.         double correct = 0, incorrect = 0;
  12.        
  13.        
  14.         while(running) {
  15.             int selection = randomNum(5) + 1;
  16.        
  17.             System.out.println("Guess what color the computer picked:\n"
  18.                 + "1.Red\n2.Green\n3.Blue\n4.Yellow\n5.Orange\n6.Purple");
  19.             int userSelection = userInput.nextInt();
  20.        
  21.             if(userSelection == selection) {
  22.            
  23.                 System.out.println("You guessed correctly !!!");
  24.                 correct++;
  25.                
  26.             } else {
  27.            
  28.                 System.out.println("You guessed incorrectly !!!");
  29.                 incorrect++;
  30.                
  31.             }
  32.            
  33.             if(counter == 10) {
  34.                 double average = averageResult(correct, incorrect);
  35.                 System.out.printf("Correct Percentage: %.2f%%", average);
  36.                 System.exit(0);
  37.             }
  38.             counter++;
  39.        
  40.         }
  41.        
  42.         userInput.close();
  43.        
  44.     }
  45.    
  46.    
  47.     public static int randomNum(int range) {
  48.        
  49.         Random rand = new Random();
  50.         int result = rand.nextInt(6) + 1;
  51.        
  52.         return result;
  53.     }
  54.  
  55.     public static double averageResult(double correct, double incorrect) {
  56.        
  57.         double result = (correct / incorrect) * 100;
  58.        
  59.         return result;
  60.        
  61.     }
  62.    
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement