Showing posts with label output. Show all posts
Showing posts with label output. Show all posts

Trick Question

Today at school we went over the answers of the test we took last time. There was one questions I got totally wrong. A couple people including myself grumbled that it was a trick question. The instructor said it came straight out of the book.

Here is the question: What does the following code snippet output:

int n=0;
for (n=4; n > 0; n--);
{
System.out.println(n);
}

Here is a hint. There is only one line in the output. At first I protested. Surely this code should have 4 lines of output. The loop iterates through four times after all.

The trick was that there was a semicolon directly after the for loop. Therefore the loop has a body which was empty. After the loop finished, n equals 0. Then the program executes one println statement. Yeah. That's an evil question. Now I know better. Watch out for the tricks.

Libjcsi to the Rescue

I have previously written about my recent exploits in the game development field. To make sure I gain Java programming skills during this exercise, I am writing the whole game in Java. So far I tried rolling my own graphics drawing code. That turned out to be difficult. Some output looked ok. Other graphics were slightly off. This was painful.

Another developer recommended that I use their graphics package libjcsi. This package covers simplifies console output for you. I tried compiling their sample program. In 10 minutes I had a graphical user interface up and running. In an hour I had a bare bones screen of my game going. I was pleasantly surprised that this package also handled keyboard input. So I quickly coded up user input to control the direction of the main character. I now got a guy running around my simple screen. Life is good.

Next I think I will try to place objects on the screen that represent gold. Then if the user walks over top the gold, I want to register that the user picked the gold up. These all seem like baby steps. But once I get the basics down, I can rocket into more complex ideas I have about my game. I will say that I still do not have the hang of Java packages. Perhaps by the time I complete my game, I will have them nailed down.