关于将颜色传递给类(Java)的困惑

时间:2015-11-06 04:12:48

标签: class methods colors

尝试编写一个程序,如果用户输入的名称是" Randy"比Java会产生0到255之间的随机数(RGB)。如果名称是" Prius"比颜色只是绿色。从那里我将那个随机数或绿色传递到我的坦克类中。

import java.awt.*;
import java.util.*;

public class Program4
{
public static void main(String[ ] args)
{
    Scanner kb = new Scanner(System.in);
    Random rand = new Random();

    System.out.print("Please enter in your name: ");
    String name = kb.nextLine();

    if(name.equalsIgnoreCase ("Randy"))
        {
            for (int i=1 ; i<= 3; i++)
                {
                int color2 = rand.nextInt(255);
                Color myColor = new Color (color2);
                Tank myTank = new Tank(myColor, 25);
                }
        }
    else if (name.equalsIgnoreCase ("Prius"))
        {
            Color myColor = new Color (0,255,0);
            Tank myTank = new Tank(myColor,25);
        }

    //create a new instance of a Tank, get its dimension
    Color myColor = new Color(255, 0, 255);
    Tank myTank = new Tank(myColor, 25);
    int dimension = myTank.getDimension();

    //create a new instance of a Landscape
    Landscape myLS = new Landscape();

    //tell the landscape to add the tank to itself
    myLS.addTank(myTank);

    //tell the tank to turn around
    myTank.turn("left");
    myTank.turn("left");

    //ask the landscape where is its green opening (as an int)
    Point greenPoint = myLS.getGreenOpening();
    int greenY = (int)greenPoint.getY();

    //tell the tank to keep moving as long as it is above the green opening
    while(myTank.getPositionY() + dimension < greenY)
        myTank.move();

    //turn left
    myTank.turn("left");

    //hopefully, move through the green wall
    for (int i=0; i<200; i++)
        myTank.move();

    Point orangePoint = myLS.getOrangeOpening();
    int orangeY = (int)orangePoint.getY();

    if (myTank.getPositionY() + dimension < orangeY)
        {
            myTank.turn("right");
            while (myTank.getPositionY() + dimension < orangeY)
            {
                myTank.move();
            }
            myTank.turn("left");
        }
    else
        {
            myTank.turn("left");
            while (myTank.getPositionY() + dimension > orangeY)
            {
                myTank.move();
            }
            myTank.turn("right");
        }
    for (int i=0 ; i<200 ; i++)
        myTank.move();

    Point targetLocation = myLS.getTargetLocation();
    int targetY = (int)targetLocation.getY();

    if (myTank.getPositionY() + dimension <targetY)
        {
            myTank.turn("right");
            while (myTank.getPositionY() + dimension < targetY + 30)
            {
                myTank.move();
            }
            myTank.turn("left");
        }
    else
        {
            myTank.turn("left");
            while (myTank.getPositionY() + dimension > targetY + 30)
            {
                myTank.move();
            }
            myTank.turn("right");
        }
    for (int i=0 ; i<500 ; i++)
        myTank.move();

   }
}

然而,程序还有更多,我只需要颜色方面的帮助。该程序编译和工作。唯一的问题是随机颜色和绿色没有传递到我的坦克类。默认坦克颜色为紫色。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

Color myColor;
Tank myTank;

if(name.equalsIgnoreCase ("Randy"))
    {
        for (int i=1 ; i<= 3; i++)
            {
            int color2 = rand.nextInt(255);
            myColor = new Color (color2);
            myTank = new Tank(myColor, 25);
            }
    }
else if (name.equalsIgnoreCase ("Prius"))
    {
        myColor = new Color (0,255,0);
        myTank = new Tank(myColor,25);
    }
else
    {
        myColor = new Color(255, 0, 255);
        myTank = new Tank(myColor, 25);
    }
int dimension = myTank.getDimension();