The colors red, blue, and yellow are known as the primary colors because they cannot be made by mixing other colors. When you mix two primary colors, you get a secondary color: When you mix red and blue, you get purple. When you mix red and yellow, you get orange. When you mix blue and yellow, you get green. Design a program that prompts the user to enter the names of two primary colors, one at a time. If the user enters anything other than "red," "blue," or "yellow," the program should print "You didn't input two primary colors." Otherwise, it should print something in the format: "When you mix red and blue, you get purple." (Assuming the user entered "red" and "blue”.)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the names of two primary colors.");
String[] primaryColors = {"red", "blue", "yellow"};
String[][] colors = {
{"red", "purple", "orange"},
{"purple", "blue", "green"},
{"orange", "green", "yellow"}};
System.out.println("First:");
String a = in.nextLine();
System.out.println("Second:");
String b = in.nextLine();
int aI = -1;
int bI = -1;
for (int i = 0; i < primaryColors.length; i++) {
if (a.equals(primaryColors[i])) {
aI = i;
}
if (b.equals(primaryColors[i])) {
bI = i;
}
}
if (aI >= 0 && bI >= 0) {
System.out.println("When you mix " + primaryColors[aI] + " and " + primaryColors[bI] + ", you get " + colors[aI][bI] + ".");
} else {
System.out.println("You didn't input two primary colors.");
}
}
}
Comments
Leave a comment