tags:

views:

61

answers:

2

Hello there, I have just started to learn the very basics of Java programming. Using a book entitled "Programming Video Games for the Evil Genius".

I have had an Illegal Start of Expression error that I can't for the life of me get rid of. I have checked the sample code from the book and mine is identical.

The error is coming from the for(int i = difficulty; i >= 0; i- - ) line.

Thanks for helping a newbie out.

import javax.swing.*;

public class S1P4

   {public static void main(String[] args) throws Exception {

    int difficulty;
    difficulty = Integer.parseInt(JOptionPane.showInputDialog("How good are you?\n"+
            "1 = Great\n"+"10 = Terrible"));

    boolean cont = false;

    do
    {

        cont = false;

        double num1 = (int)(Math.round(Math.random()*10));

        double num2;
        do
        {
            num2 = (int)(Math.round(Math.random()*10));
        }
        while(num2==0.0);

        int sign = (int)(Math.round(Math.random()*3));

        double answer;

        System.out.println("\n\n*****");

        if(sign==0)
        {
            System.out.println(num1+" times "+num2);
            answer = num1*num2;
        }
        else if(sign==1)
        {
            System.out.println(num1+" divided by"+num2);
            answer = num1/num2;
        }
        else if(sign==1)
        {
            System.out.println(num1+" plus "+num2);
            answer = num1+num2;
        }
        else if(sign==1)
        {
            System.out.println(num1+" minus "+num2);
            answer = num1-num2;
        }
        else
        {
            System.out.println(num1+" % "+num2);
            answer = num1%num2;
        }

        System.out.println("*****\n");

        for(int i = difficulty; i >= 0; i- - )

        {
            System.out.println(i+"...");

            Thread.sleep(500);
        }
        System.out.println("ANSWER: "+answer);

        String again;
        again = JOptionPane.showInputDialog("Play again?");

        if(again.equals("yes"))
            cont = true;
    }

    while(cont);

} }

+1  A: 

Have you tried i-- instead of i- -.

There seems to be an extra space.

Dipstick
Thanks! Yeah that fixed it, for some reason in the book where ever -- is listed its shown with a space between the two - signs.Yet ++ is always listed together.
Kraivyne
@Kraivyne - yet another reason to dump the book. It obviously wasn't copy-edited properly.
Stephen C
+5  A: 

You had accidentally introduced a space, separating the -- (JLS 15.14.3 Postfix decrement operator) into two tokens - -. This is what caused the syntax error.

By the way, if this code as written is even close to identical to what's in the book, then I suggest getting another book. That code is horribly written. The if-else are ineffective: the third case and beyond are unreachable.

Let's also look at this code:

int sign = (int)(Math.round(Math.random()*3));

So... we want a random int between 0..3? Why not use java.util.Random.nextInt(int n)?

Not to mention, if we're going to switch (JLS 14.11) on sign, don't we need 5 different values instead of 4? Since there are 5 operators?

And that's just the obvious logical errors. There are many stylistic problems with the code as well.

Horrible book.

polygenelubricants
+1 for the last two sentences!
Martijn Courteaux
This is exactly word for word how it is written in the book. That's actually the reason it gave me an error. I picked up this book because I read online reviews that stated it was good for a beginner. Sadly As I go through it, I seem to find more and more mistakes.
Kraivyne