Write a program that will be used in a robot that categorizes items by their color. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a program that will be used in a robot that categorizes items by their color.

Each color corresponds to a box with a specific number. For simplicity, our program will handle 3 colors: red goes to box #1 green goes to box #2 black goes to box #3 Your program needs to take a color as input and output the corresponding box number. Sample Input green Sample Output 2 https://code.sololearn.com/c9B52Ml34dAf/?ref=app

14th Dec 2021, 2:42 PM
anuja jangale
3 Answers
+ 5
You check for variable named red, greed, black exept string, so wrap it in quote as Jay Matthews said
14th Dec 2021, 3:43 PM
PanicS
PanicS - avatar
+ 3
Hello, welcome to sololearn Q&A. To take most from community please tag language, and post your code attempt.
14th Dec 2021, 2:50 PM
PanicS
PanicS - avatar
+ 2
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String color = scanner.nextLine().trim().toLowerCase(); int boxNumber = categorizeColor(color); System.out.println(boxNumber); } public static int categorizeColor(String color) { switch(color) { case "red": return 1; case "green": return 2; case "black": return 3; default: return -1; // Invalid color } } } correct
19th Feb 2024, 3:27 PM
Irfan Alam
Irfan Alam - avatar