--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
----R---------------
--------------------
--------------------
--------------------
With the 'R' being the random letter (actually representing a color, in this case Red) and the random position being (5,17). I can generate the random letter, but I've no clue how to put it in a random spot on the grid. I'm thinking I have to do something along the lines of
if something print the letter else print "-"but i'm not sure what
My code for the program so far is...
import javax.swing.JOptionPane;
public class Homework5
{
public static void main (String[]args)
{
int carColor = 0, color = 0;
char colorChar = 0;
System.out.println("Car Stats:");
color = 1 + (int) (Math.random() * 5);
paint(color);
int posHorizontal = 0, posVertical = 0;
posHorizontal = 1 + (int) (Math.random() * 20);
posVertical = 1 + (int) (Math.random() * 20);
System.out.print("\n Location: " + posHorizontal + ", " + posVertical);
System.out.println();
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
System.out.print("-");
}
System.out.println();
}
}
public static int paint(int color)
{
char colorChar = 0;
System.out.print(" Color: ");
switch (color) {
case 1: System.out.print("Red");
colorChar = 'R';
break;
case 2: System.out.print("Green");
colorChar = 'G';
break;
case 3: System.out.print("Blue");
colorChar = 'B';
break;
case 4: System.out.print("White");
colorChar = 'W';
break;
case 5: System.out.print("Silver");
colorChar = 'S';
break;
}
System.out.println();
return color;
}
}
Another problem I've got is assigning the color for the initial readout ("Color: Red") /and/ the char to be placed in the grid ('R'). I don't know if I've done it right since I can't figure out how to put the char on the grid in the first place.
Also I know my nested for loop is most likely in the wrong place. Should it be somewhere in one of the methods I know I need to create, for vertical or horizontal position of the letter?
Thanks for any help! and sorry for anything that may not make sense in my question..

New Topic/Question
Reply




MultiQuote




|