Eclipse上java中的随机颜色打印

时间:2015-01-19 07:04:48

标签: java random colors rgb

我创建了一个随机颜色游戏,部分代码是:

public static void getARandomColor() 
{
    System.out.println();
    System.out.println("Your random color is...");
    // red
    MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
    RED = MY_RANDOM;
    // green
    MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
    GREEN = MY_RANDOM;
    // blue
    MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
    BLUE = MY_RANDOM;
    // combinations...
    System.out.println("Your color is r: " + RED + ", g: " + GREEN
            + ", b: " + BLUE + "!");
    System.out.println();
}

所以所有内容都有效,但是我希望更进一步,在中文之后添加文字     " System.out.println("你的颜色是r:" + RED +",g:" + GREEN                     +",b:" + BLUE +"!");"成为随机颜色的线。

完整代码

     import java.awt.Color;
    import java.util.Random;
    import java.util.Scanner;

    import javax.swing.JTextArea;

    /**
     * My RNG game has multiple choices; Array, Coins, and colors.
     */
    public class RandomNumberGenerator 
    {
        // all colors available
        private static final int COLOR_MAX = 256;
        private static int RED, GREEN, BLUE;
        private static int MY_RANDOM;
        static Scanner MY_CONSOLE = new Scanner(System.in);
        static Random RANDOM_NUMBER = new Random();
        private static int[] RANDOM_NUM_ARRAY;

        /**
         * Make the game work! Main System of the game.
         */
        public static void main(String[] args) 
        {

            while (true) 
            {
                // menu intro
                System.out.println("Welcome to the Random Generation Game!");
                System.out.println("Chose an option below: ");
                System.out.println();
                // menu
                System.out.println("1. Generate a random array of numbers!");
                System.out.println("2. Flip a coin!");
                System.out.println("3. Create a random color!");
                System.out.println("4. Exit!");
                int option = MY_CONSOLE.nextInt();
                // which option is chosen?
                // random array game
                if (option == 1) 
                {
                    createYourRandomArray();
                }
                // coin game
                else if (option == 2) 
                {
                    flipTheCoinGame();
                }
                // color game
                else if (option == 3) 
                {
                    getARandomColor();
                }
                // quit?
                // I tried to put 1, 2, 3 and 4 into constants but then the code
                // would not read the value behind the constant and would go into
                // my else...
                else if (option == 4) 
                {
                    break;
                }
                // Error!
                else 
                {
                    System.out.println("Invalid! Choose 1-4!");
                }
            }

            // close console
            MY_CONSOLE.close();

            System.out.println();
            // end of game text
            System.out.println("See ya later!");
        }

        // random array game works
        /**
         * The array of random numbers created here.
         */
        public static void createYourRandomArray() 
        {
            // elements?
            System.out.println("How many elements do you want in your array?");
            int element = MY_CONSOLE.nextInt();
            MY_CONSOLE.nextLine();
            // min?
            System.out.println("Min value?");
            int myMin = MY_CONSOLE.nextInt();
            MY_CONSOLE.nextLine();
            // max?
            System.out.println("Max?");
            int myMax = MY_CONSOLE.nextInt();
            MY_CONSOLE.nextLine();
            // random works
            RANDOM_NUM_ARRAY = new int[element];
            // open
            System.out.println();
            System.out.print("[");
            //
            for (int i = 0; i < RANDOM_NUM_ARRAY.length; i++) 
            {
                MY_RANDOM = RANDOM_NUMBER.nextInt(myMax - myMin + 1);
                MY_RANDOM += myMin;

                RANDOM_NUM_ARRAY[i]++;
                RANDOM_NUM_ARRAY[i] = MY_RANDOM;

                if (i < RANDOM_NUM_ARRAY.length - 1) 
                {
                    System.out.print(RANDOM_NUM_ARRAY[i] + ", ");
                } 
                else 
                {
                    System.out.print(RANDOM_NUM_ARRAY[i]);
                }

            }

            System.out.println("]");
            System.out.println();
        }

        /**
         * The Flip coin part of game created here.
         */
        public static void flipTheCoinGame() 
        {
            int coinTail = 0;
            int coinHead = 0;

            System.out.println("How many times would you like to flip the coin?");
            int youFlip = MY_CONSOLE.nextInt();
            System.out.println();

            for (int i = 0; i < youFlip; i++) 
            {
                MY_RANDOM = RANDOM_NUMBER.nextInt(2);
                if (MY_RANDOM == 1) 
                {
                    System.out.print("heads, ");
                    coinHead++;
                }
                // if not heads then tails
                else 
                {
                    System.out.print("tails, ");
                    coinTail++;
                }
            }

            System.out.println();
            System.out.println("You flipped " + coinHead + " heads and " + coinTail
                    + " tails!");
            System.out.println();
        }

        // Generates color in RGB form.
        /**
         * Random color generator. Creates a random RGB.
         */
        public static void getARandomColor() 
        {
            System.out.println();
            System.out.println("Your random color is...");
            // red
            MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
            RED = MY_RANDOM;
            // green
            MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
            GREEN = MY_RANDOM;
            // blue
            MY_RANDOM = RANDOM_NUMBER.nextInt(COLOR_MAX);
            BLUE = MY_RANDOM;
            // combinations...
            System.out.println("Your color is r: " + RED + ", g: " + GREEN
                    + ", b: " + BLUE + "!");
            System.out.println();
        }
    }

1 个答案:

答案 0 :(得分:1)

我认为没有方法可以在java中获取颜色名称。所以你创建了以下方法。

String getColorName(int r, int g, int b){switch(r){case 0: switch(g){
    case 0: switch(b)
            {
            case 0: return "black";
            break;
            case 128: return "Navy";
            break;
            case 255: return "Blue";
            break;
            //continue for all color

            }
    }
    }

您可以参考此颜色名称和RGB值。 enter link description here