2015-11-06 34 views
0

試圖編寫一個程序,其中如果用戶輸入的名稱比「Randy」要比Java生成0到255(RGB)之間的隨機數。如果名稱是「Prius」,則顏色僅爲綠色。從那裏我會通過隨機數或綠色到我的坦克班。關於將顏色傳遞給類(Java)

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

    } 
} 

還有更多的程序,但我只是需要顏色的幫助。該程序編譯和工作。唯一的問題是隨機的顏色和綠色沒有被傳遞到我的坦克班。默認的坦克顏色是紫色的。

謝謝你的幫助。

回答

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(); 
+0

我試過,但是,最後一個不會工作,因爲我有其他代碼,如「int dimension = myTank.getDimension();」如果我這樣做,我會得到28個錯誤,說它找不到myTank符號。 – Paincakes

+0

改變了..你可以試試嗎?你會得到錯誤,因爲你已經在if-else塊內初始化了mytank – user9480