【问题标题】:Integer Value to Color (JAVA)整数值到颜色 (JAVA)
【发布时间】:2017-04-29 18:15:09
【问题描述】:

我制作了一个 2D 数组,随机填充 0 到用户选择的数字(最多 6 个)之间的数字。我想用颜色更改这些数字,但是当我尝试将每个值分配给颜色时,我收到无法从 int 转换为颜色的消息......有什么建议吗?因为我真的卡住了

    public static void rellenarTablero(int[][] tablero) {
    System.out.println("Introduzca el numero de colores (de 2 a 6): ");
    Scanner in = new Scanner(System.in);
    int colores = in.nextInt();
    while(colores<2||colores>6){
        System.out.println("Elija un numero valido:");
        colores = in.nextInt();
    }
    for (int x = 0; x < tablero.length; x++) {
        for (int y = 0; y < tablero[x].length; y++) {
            tablero[x][y] =1+(int)(Math.random()*(colores));
            if(x==1){
                x=Color.BLUE;
            }if(y==1){
                y=Color.BLUE;
            }
            if(x==2){
                x=Color.RED;
            } 
            if(y==2){
                y=Color.RED;
            }
            if(x==3){
                x=Color.GREEN;
            }
            if(y==3){
                y=Color.GREEN;
            }
        }
    }
}

【问题讨论】:

  • 这些数字应该如何转换成颜色?例如,如果用户输入 3,那么该结果应该是什么颜色?
  • 您可以从Map&lt;Integer, Color&gt; 地图开始...
  • 好吧,分配是无关紧要的,让我们把 1=blue, 2=red & 3=green 用于这个例子,代码在编辑中,对不起
  • 我对这里的功能感到非常困惑。如果将x 定义为int 类型,则不能将其分配为Color 类型。
  • 所以我必须将x 定义为color 而不是int 类型吗?那么我怎么能随机填充我的数组呢?对不起,如果这是一个愚蠢的问题,但我只是一个初学者

标签: java arrays colors assign


【解决方案1】:

如果我理解正确,这应该可以修复您的代码。我插入了有关我所做的最重要更改的 cmets。

// if the table is to be filled with colors, declare it an table of Color
public static void rellenarTablero(Color[][] tablero) {
    System.out.println("Introduzca el numero de colores (de 2 a 6): ");
    Scanner in = new Scanner(System.in);
    int colores = in.nextInt();
    while (colores < 2 || colores > 6) {
        System.out.println("Elija un numero valido:");
        colores = in.nextInt();
    }
    // I prefer to use a Random object for random integers, it’s a matter of taste
    Random rand = new Random();
    for (int x = 0; x < tablero.length; x++) {
        for (int y = 0; y < tablero[x].length; y++) {
            int numeroAleatorioDeColor = 1 + rand.nextInt(3);
            // prefer if-else to make sure exactly one case is chosen
            if (numeroAleatorioDeColor == 1) {
                // fill the color into the table (not the int)
                tablero[x][y] = Color.BLUE;
            }
            else if (numeroAleatorioDeColor == 2) {
                tablero[x][y] = Color.RED;
            }
            else if (numeroAleatorioDeColor == 3) {
                tablero[x][y] = Color.GREEN;
            }
            else {
                System.err.println("Error interno " + numeroAleatorioDeColor);
            }
        }
    }
}

从一开始就将您必须选择的所有颜色放入一个单独的数组中可能更容易:

static final Color[] todosColores = { Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, Color.BLACK, Color.ORANGE };

如果您有多种颜色可供选择,则尤其如此。现在你可以这样做了:

            // number to use for array index; should start from 0, so don’t add 1
            int numeroAleatorioDeColor = rand.nextInt(todosColores.length);
            tablero[x][y] = todosColores[numeroAleatorioDeColor];

【讨论】:

    猜你喜欢
    • 2020-02-20
    • 2016-02-01
    • 2016-10-14
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 2011-08-08
    相关资源
    最近更新 更多