Notice: Undefined offset: 13242321 in /var/www/html/qa-external/qa-external-users.php on line 744
how to make java randomly pick a color from red green blue or yellow and ask if the color is nice - OnlineGDB Q&A
Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to make java randomly pick a color from red green blue or yellow and ask if the color is nice

+5 votes
asked Oct 11, 2024 by Ariana B2025 (170 points)

3 Answers

+1 vote
answered Oct 11, 2024 by artham Mukesh (160 points)
import java.util.Random;
import java.util.Scanner;

public class RandomColorPicker {
    public static void main(String[] args) {
        // Array of colors
        String[] colors = {"Red", "Green", "Blue", "Yellow"};
        
        // Create a Random object
        Random random = new Random();
        
        // Pick a random color from the array
        String randomColor = colors[random.nextInt(colors.length)];
        
        // Display the picked color
        System.out.println("Do you like the color " + randomColor + "? (yes/no)");
        
        // Create a Scanner object for user input
        Scanner scanner = new Scanner(System.in);
        
        // Get the user's response
        String response = scanner.nextLine();
        
        // Respond based on user's input
        if (response.equalsIgnoreCase("yes")) {
            System.out.println("Great! " + randomColor + " is a cool color.");
        } else if (response.equalsIgnoreCase("no")) {
            System.out.println("Oh no! Maybe you'll like another color.");
        } else {
            System.out.println("I didn't understand your answer, but colors are fun!");
        }
        
        // Close the scanner
        scanner.close();
    }
}
0 votes
answered Oct 11, 2024 by Bharathi (140 points)
public class RandomColorPicker{

public static void main (Strinf args[]){
Random random=new Random();

String[] colors={"Red","Green","Blue","Yellow"};

int randomIndex=random.nextInt(colors.length);

string randomColor=colors[randomIndex];

System.out.println("The color is"+randomColor+".Do you like this color?(yes/no);

Scanner response=scanner.nextline();

(response.equalsIgnoreCase("yes")){
System.out.println("Great! You like"+randomColor+".");

}

else{

System.out.println("Oh no! You don't like"+randomColor+".");

}

scammer.close();

}

}
0 votes
answered Oct 11, 2024 by Nikhila Nandyala (410 points)
import java.util.Random;
import java.util.Scanner;


public class RandomColorPicker {
        Public static void main(string[]args){
         String[]colors ={"Red","Green","Blue","Yellow",};
         Random random= new Random();
         String randomColor=colors[ random.nextInt(colors.length)];
         System.out.println("Do you think the color"+randomColor+"is nice?(Yes/no)");
           Scanner scanner=new Scanner(System.in);
            String response= scanner.nextLine();
           If(response.equalsIngoreCase("yes"){
               System.out.println("Oh no!You don't like "+randomColor+".");
     }
       scanner.close();
      }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...