我在我的AP计算机科学课中使用GridWorld,我们正在做的一部分是改变bug(一个对象)的颜色.我找到了一个非常基本的方法来做这个,但我想尝试合并字符串,以允许用户输入他想要的bug颜色,而不必输入RGB的数字值.通过输入"red"并使字符串存储,我能够将字符串值作为我想要的颜色.但是如何将该字符串转换为颜色呢?我不确定我是否说得足够清楚,但是我附上了我的代码,所以希望有人会理解并且可以提供帮助.
Color red = new Color (255, 0, 0);
Color green = new Color (0, 255, 0);
Color blue = new Color (0, 0, 255);
System.out.println("What color would you like the first bug to be? (red, green, blue)");
String name = "color1";
String color1 = keyboard.next();
if (color1 == "red")
{
world.add (new Location (bugx1, bugy1), new Bug(red));
}
if (color1 == "blue")
{
world.add (new Location (bugx1, bugy1), new Bug(blue));
}
if (color1 == "green")
{
world.add (new Location …Run Code Online (Sandbox Code Playgroud)