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
4 Answers
+ 5
You check for variable named red, greed, black exept string, so wrap it in quote as Jay Matthews said
+ 3
Hello, welcome to sololearn Q&A.
To take most from community please tag language, and post your code attempt.
+ 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
0
This is correct.
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
}
}
}