创建一个switch语句,将Color类的数据类型转换为String类型。

huangapple go评论61阅读模式
英文:

Creating a switch statement to convert Color class data types to String type

问题

我目前正在尝试创建一个开关语句通过使用getColor()将颜色作为字符串输入将Color.red或其他颜色设置为变量color的Primative Color类型如果我甚至不能正确提问我在此提前道歉我在4周前开始编程

public class ChooseAColor {
    private Color color;
    
    // 默认颜色
    public Color defaultColor() {
        this.color = Color.red;
    }
    
    public Color getColor(String color) {
        switch (color) {
            case "Red":
                return java.awt.Color.red;
            case "Black":
                return java.awt.Color.black;
            case "Blue":
                return java.awt.Color.blue;
            default:
                return null;
        }
    }
}

然后尝试调用和打印

public class TestChooseAColor {
    ChooseAColor myColor = new ChooseAColor();

    System.out.println("My favorite color is: " + myColor.getColor("Red"));
}

我期望的输出是:

我最喜欢的颜色是红色

然而,我得到了:

java: 不兼容的类型: 无法将java.awt.Color转换为int
英文:

I am currently trying to create a switch statement that upon using getColor(), typing the color as a string sets the Primative Color type of Color.red (or other) to variable color. I apologize in advance if I am unable to even ask the question properly. I started programming 4 weeks ago.

public class ChooseAColor {
private Color color

//default color
public Color defaultColor(){
this.color = Color.red


public Color getColor(Color color){
    switch (color){
        case "Red":
            return color = java.awt.Color.red;
            break;
        case "Black":
            return java.awt.Color.black;
            break;
        case "Blue":
            return color = java.awt.Color.blue;
        default:
            break;
        }

    }
}

Then trying to call and print

public class TestChooseAColor{
ChooseAColor myColor = new ChooseAColor();

System.out.println("My favorite color is: " + myColor.getColor("Red")

I am expecting an output of

My favorite color is red

However, I am getting

java: incompatible types: java.awt.Color cannot be converted to int

答案1

得分: 1

快要完成了。看起来问题出在你的方法上。为什么不把参数从public Color getColor(Color color){改成public Color getColor(String color){呢?

毕竟,在你的打印消息中,你传递给方法的是一个字符串,而不是颜色。所以,如果你想传递一个字符串,那么方法的参数应该接受字符串才有意义。

另外,你可能需要处理一下那些返回语句。为什么在返回时还要设置颜色呢?为什么不直接返回呢?

将这个:

return color = java.awt.Color.red;

改成这个:

return java.awt.Color.red;

最后,看起来你有一些不必要的break;语句。如果你正在返回一个值,那么就不需要使用break,对吗?

但是不要忘记你确实需要返回一个值,所以似乎那个default情况需要用一些内容来替代break;

也许你可以使用你代码中的那个defaultColor()方法?(像之前提到的,加上正确的返回语句)

英文:

Almost there. Looks like the problem is with your method. Why don't you change the parameter from public Color getColor(Color color){ into public Color getColor(String color){?

After all, in your print message, you are giving your method a String, not a Color. So if you want to give it a String, then it would make sense that your method parameter should accept Strings.

Also, you might want to do something about those return statements. Why are you trying to set color while returning? Why not just return?

Instead of this:

return color = java.awt.Color.red;

Try this:

return java.awt.Color.red;

Lastly, it looks like you have some unnecessary break; statements. If you are returning a value, then there is no need for a break, right?

But don't forget that you DO need to return a value, so it looks like that default will need something INSTEAD of a break;

Maybe you can use that defaultColor() method from your code? (with a proper return statement, as mentioned earlier)

huangapple
  • 本文由 发表于 2020年9月14日 11:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63877713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定