本文介绍了在Eclipse上的随机颜色打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  public static void getARandomColor $ b {
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;
// combine ...
System.out.println(你的颜色是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;

/ **
*我的RNG游戏有多个选择;数组,硬币和颜色。
* /
public class RandomNumberGenerator
{
//所有颜色可用
private static final int COLOR_MAX = 256;
private static int RED,GREEN,BLUE;
private static int MY_RANDOM;
static Sc​​anner MY_CONSOLE = new Scanner(System.in);
static RANDOM_NUMBER = new Random();
private static int [] RANDOM_NUM_ARRAY;

/ **
*让游戏工作!主系统的游戏。
* /
public static void main(String [] args)
{

while(true)
{
//菜单简介
System.out.println(Welcome to the Random Generation Game!);
System.out.println(Chose an option below:);
System.out.println();
// menu
System.out.println(1。生成一个随机数字数组!
System.out.println(2.翻转硬币!);
System.out.println(3。创建随机颜色!);
System.out.println(4. Exit!);
int option = MY_CONSOLE.nextInt();
//选择哪个选项?
//随机数组游戏
if(option == 1)
{
createYourRandomArray();
}
//硬币游戏
else if(option == 2)
{
flipTheCoinGame();
}
//颜色游戏
else if(option == 3)
{
getARandomColor();
}
//退出?
//我试图将1,2,3和4放入常量,但是代码
//不会读取常量后面的值,而是进入
// my else。 ..
else if(option == 4)
{
break;
}
//错误!
else
{
System.out.println(Invalid!Choose 1-4!);
}
}

//关闭控制台
MY_CONSOLE.close();

System.out.println();
//游戏结束文本
System.out.println(see ya later!);
}

//随机数组游戏工程
/ **
*此处创建的随机数组数组。
* /
public static void createYourRandomArray()
{
// elements?
System.out.println(你想在你的数组中有多少个元素?
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];
//打开
System.out.println();
System.out.print([);
//
for(int i = 0; i {
MY_RANDOM = RANDOM_NUMBER.nextInt(myMax - myMin + 1);
MY_RANDOM + = myMin;

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

if(i {
System.out.print(RANDOM_NUM_ARRAY [i] +,);
}
else
{
System.out.print(RANDOM_NUM_ARRAY [i]);
}

}

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

/ **
*这里创建的Flip硬币游戏部分。
* /
public static void flipTheCoinGame()
{
int coinTail = 0;
int coinHead = 0;

System.out.println(你想要多少次翻转硬币?
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 ++;
}
//如果不是头,那么tails
else
{
System.out.print(tails,);
coinTail ++;
}
}

System.out.println();
System.out.println(你翻转+ coinHead +头和+ coinTail
+尾!
System.out.println();
}

//生成RGB形式的颜色。
/ **
*随机颜色生成器。创建随机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;
// combine ...
System.out.println(你的颜色是r:+ RED +,g:+ GREEN
+,b:+ BLUE + !);
System.out.println();
}
}


解决方案

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

  String getColorName(int r,int g,int b){switch(r){case 0:switch(g){
case 0:switch(b)
{
case 0:returnblack
break;
case 128:returnNavy;
break;
case 255:returnBlue;
break;
//继续所有颜色$​​ b
$ b}
}
}

你可以参考它的颜色名称和RGB值。


I have created a Random Color game and part of the code goes is:

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();
}

So everything works that is shown, but I want to go further and add text right after the
"System.out.println("Your color is r: " + RED + ", g: " + GREEN + ", b: " + BLUE + "!");" line which becomes the random color given.

full code

     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();
        }
    }
解决方案

I think there in no method to get name of color in java. so you create the following method.

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

            }
    }
    }

you can refer this for color names and RGB values.enter link description here

这篇关于在Eclipse上的随机颜色打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 05:02