How to change text color (output) in Java

Recommended Videos

TheHorizon

New member
Dec 17, 2008
217
0
0
Hey, I'm making a program for my high school Comp Sci class with a partner.
Because we're beasts and finished the project on the second day we were looking to see if we could do any extra improvisation to keep us entertained and for possible extra credit.

So I was wondering if you guys knew how to do such a thing.

If anyone wants to see our awesome program here it is:

(Warning: contains extreme awesomeness)

import java.util.*;
public class luckyStripes {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random generator = new Random();
int[][] board = new int [5][5];
int counter = 0;
int randRow;
int randCol;
int score = 0;
int totalScore = 0;
String cont = "Y";

while (cont.equals ("Y")){
while (counter < 5){
for (int row = 0; row < 5; row++){
for (int col = 0; col < 5; col++){
if (row == 0 || row == 4 || col == 0 || col == 4){
board[row][col] = 1;
}
}
}
for (int row = 1; row < 4; row++){
for(int col = 1 ; col < 4; col++){
board[row][col] = 2;
}
}
board [2][2] = 3;

randRow = generator.nextInt(5);
randCol = generator.nextInt(5);

if (board[randRow][randCol] == 1){
score += 1;
totalScore += 1;
}
if (board[randRow][randCol] == 2){
score += 2;
totalScore += 2;
}
if (board[randRow][randCol] == 3){
score += 3;
totalScore += 3;
}

board[randRow][randCol] = 0;

for (int row = 0; row < 5; row++){
for (int col = 0; col < 5; col++){
System.out.print(board[row][col] + "\t");
}
System.out.println("");
}
System.out.println("");
System.out.println("The score so far is: " + score);
System.out.println("The total score for all games so far is: " + totalScore);
System.out.println("");

counter++;
}
score = 0;
counter = 0;
System.out.print("Would you like to play again? (Y/N): ");
cont = keyboard.nextLine();
cont = cont.toUpperCase();
while (!(cont.equals("Y") || (cont.equals("N")))){
System.out.println("You must input either letter Y or N!");
System.out.print("Would you like to play again? (Y/N): ");
cont = keyboard.nextLine();
cont = cont.toUpperCase();
}
if (cont.equals("N"))
System.out.println("Thanks for playing!");
}
}
}
 

Overlord_Dave

New member
Mar 2, 2009
295
0
0
all i can say is that it might be better to make cont a boolean, and then after checking the input set it appropriately. It does exactly the same thing but if you were to change the input to something other than pressing Y or N it would require less changing of the code. Just pointless future-proofing really, but in my opinion technically better.

EDIT: i've compiled it and i'm afraid i've got no idea what it actually does.

Also, you can't change any formatting when using System.out. The console just prints plain text.
 

theklng

New member
May 1, 2008
1,229
0
0
document your code! i realize that it's a simple program, but good programming practices are better than being good at programming. also what dave said. as for topic, look up the java api and use a boolean if you just want to switch between 2 colors or a switch if more.
 

TheHorizon

New member
Dec 17, 2008
217
0
0
Overlord_Dave said:
EDIT: i've compiled it and i'm afraid i've got no idea what it actually does.
Sorry, my partner was talking to me while I posted this.

What the program does is it is like a penny toss game.
The user itself doesn't actually do anything (Wasn't my decision) other than say if they want to "play" it again.
You "throw" a coin onto the grid (represented by the zero) and you get points depending on what you land it on.

EDIT: This is my first year of computer science (and I'm only in high school) so I don't really know what you mean when you say "document your code".
Do you mean put comments around everything? If so, that's a good idea. I'm not really sure why I haven't been doing that.
 

Overlord_Dave

New member
Mar 2, 2009
295
0
0
well documented code should get you extra marks. in the real world comments are essential, and the course may emphasize this. you may also want to investigate what a 'javadoc' is. I never bothered and now the projects i'm working on are too large to create one.