Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main
  5. {
  6.     static int colourNum, correctGuesses = 0;;
  7.     static String colour;
  8.     static Scanner kbd = new Scanner(System.in);
  9.     static Random rnd = new Random();
  10.     public static void main(String[] args)
  11.     {
  12.         userValidation();
  13.        
  14.         System.out.println("You have guessed " + correctGuesses + " colours correct!");
  15.     }
  16.     /**
  17.      
  18.         The userChoice method get's their color guess while determining if it is correct to the computer's
  19.      
  20.      */
  21.     public static void userChoice()
  22.     {
  23.         colourNum = rnd.nextInt(5);
  24.         System.out.println("Enter the colour selected by the computer:");
  25.         colour = kbd.nextLine();
  26.         userCorrect(colour, colourNum);
  27.         computerChoice(colourNum);
  28.     }
  29.     /**
  30.      
  31.         The computerChoice method prints the color the computer chooses.
  32.         @param colourNum The random color selected by the computer
  33.  
  34.      */
  35.     public static void computerChoice(int colourNum)
  36.     {
  37.         switch(colourNum)
  38.         {
  39.             case(0):
  40.                 System.out.println("The colour is Red");
  41.                 break;
  42.             case(1):
  43.                 System.out.println("The colour is Green");
  44.                 break;
  45.             case(2):
  46.                 System.out.println("The new colour is Blue");
  47.                 break;
  48.             case(3):
  49.                 System.out.println("The new colour is Orange");
  50.                 break;
  51.             case(4):
  52.                 System.out.println("The new colour is Yellow");
  53.                 break;
  54.         }
  55.     }
  56.     public static boolean userCorrect(String colour, int colourNum)
  57.     {
  58.         switch(colour.toLowerCase())
  59.         {
  60.             case("red"):
  61.                 if(colourNum == 0)
  62.                 {
  63.                     System.out.println("Correct!");
  64.                     correctGuesses ++;
  65.                     return true;
  66.                 }
  67.                     break;
  68.             case("green"):
  69.                 if(colourNum == 1)
  70.                 {
  71.                     System.out.println("Correct!");
  72.                     correctGuesses ++;
  73.                     return true;
  74.                 }
  75.                     break;
  76.             case("blue"):
  77.                 if(colourNum == 2)
  78.                 {
  79.                     System.out.println("Correct!");
  80.                     correctGuesses ++;
  81.                     return true;
  82.                 }
  83.                     break;
  84.             case("orange"):
  85.                 if(colourNum == 3)
  86.                 {
  87.                     System.out.println("Correct!");
  88.                     correctGuesses ++;
  89.                     return true;
  90.                 }
  91.                     break;
  92.             case("yellow"):
  93.                 if(colourNum == 4)
  94.                 {
  95.                     System.out.println("Correct!");
  96.                     correctGuesses ++;
  97.                     return true;
  98.                 }
  99.                     break;
  100.         }
  101.         return false;
  102.     }
  103.     public static void userValidation()
  104.     {
  105.         boolean valid = false;
  106.         while(!valid)
  107.         {
  108.             for(int i = 0; i < 5;i++)
  109.             {
  110.             userChoice();
  111.             }
  112.             valid = true;
  113.         }
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement