开发者

Illegal Start of Expression

开发者 https://www.devze.com 2022-12-29 07:27 出处:网络
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 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/nu开发者_如何转开发m2;
        }
        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);

} }


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.


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

There seems to be an extra space.

0

精彩评论

暂无评论...
验证码 换一张
取 消