'How would I go about breaking down this code into a new method in this Java program?

I've got an assignment that requires me to have the user guess a random color generated by the program, then display the total results. My code works fine, but I'm wanting to break it down into another method. Specifically, the section that receives input from the user. I want that in its own method, then the main method only displaying results. I tried creating a new object for the user's color choice, but that didn't work. I'm not sure what to do next. Thank you for your time!

import java.util.Scanner;
import java.util.Random;

public class ESP 

{
    public static void main(String[] args)
    {
        String colorChoice;
        int correct = 0, incorrect = 0;

        Scanner keyboard = new Scanner(System.in);

        for(int i = 1; i <= 10; i++)
        {    
            System.out.print("Choose either red, green, blue, orange or yellow! ");
            colorChoice = keyboard.nextLine();

            System.out.println("You chose: " + colorChoice);
            String computerChoice = computerColorChoice();
            System.out.println("The computer chose: " + computerChoice);

            if(colorChoice.equalsIgnoreCase(computerChoice))
                {
                    correct++;
                }
            else
                {
                    incorrect++;
                }  
        }

        System.out.println("Amount of times you guessed correctly: " + correct);
        System.out.println("Amount of times you guess incorrectly: " + incorrect);
    }
    public static String computerColorChoice()
    {
        String randomColor;
        int randomNumber;

        Random random = new Random();
        randomNumber = random.nextInt(4);

        switch (randomNumber)
        {
            case 0: 
                randomColor = "Red";
                break;
            case 1:
                randomColor = "Green";
                break;
            case 2:
                randomColor = "Blue";
                break;
            case 3:
                randomColor = "Orange";
                break;
            case 4:
                randomColor = "Yellow";
                break;
            default:
                randomColor = " ";      
        }
        return randomColor;
    }
    
}


Solution 1:[1]

You can add a method for the user choice:

public static String userColorChoice()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Choose either red, green, blue, orange or yellow! ");
    String userColorChoice = keyboard.nextLine();
    System.out.println("You chose: " + userColorChoice);
    return userColorChoice;
}

Also you can update your computerColorChoice as follows:

public static String computerColorChoice()
{
    String randomColor;
    int randomNumber;

    Random random = new Random();
    randomNumber = random.nextInt(4);

    switch (randomNumber)
    {
        case 0:
            randomColor = "Red";
            break;
        case 1:
            randomColor = "Green";
            break;
        case 2:
            randomColor = "Blue";
            break;
        case 3:
            randomColor = "Orange";
            break;
        case 4:
            randomColor = "Yellow";
            break;
        default:
            randomColor = " ";
    }
    System.out.println("The computer chose: " + randomColor);
    return randomColor;
}

And finally update your main:

public static void main(String[] args)
{
    int correct = 0, incorrect = 0;
    for(int i = 1; i <= 10; i++)
    {
        if(userColorChoice().equalsIgnoreCase(computerColorChoice()))
            correct++;
        else
            incorrect++;
    }
    System.out.println("Amount of times you guessed correctly: " + correct);
    System.out.println("Amount of times you guess incorrectly: " + incorrect);
}

I hope this help you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Isma