-4

When I run my code, it gives me an error that says, "Syntax error on token "{", throw expected after this token." The error is on line 7's code.

class WhileLoopTest {

  public static void main(String[] args){

  apple = 0;

  while (apple = 0) {

    (int)(Math.random( )*(60) + 5);
    return;


  }


  }

}
Bryan Gomez
  • 1
  • 1
  • 1

3 Answers3

0

on the line while (apple = 0) you are setting the variable instead of declaring it. The while loop expects that you pass it a boolean. You are probably trying to use the comparison equals ==. The full line should read while (apple == 0).

ug_
  • 11,267
  • 2
  • 35
  • 52
0

You need to add an extra equals sign to the condition within the while statement (at the moment you are assigning the value of 0 to apple, instead of texting if it is equal), so it looks like this

while(apple == 0){

Pleas note that the while loop has no function at all, since you are returning within the loop. This will stop your program execution as you are returning from the main method. The computation of a random number doesn't serve a purpose here as you aren't assigning a variable to it or printing it.

Also, you are not defining a type for the apple variable. Try making it of type int.

int apple = 0;

I suggest that you look up some tutorials on java as you seem to misunderstand several concepts within the language.

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
0

First , you need to define a type for your variable apple because Java is statically type

apple = 0;

Read more About Statically typed vs Dynamically typed

change to

int apple = 0;

Second, (int)(Math.random( )*(60) + 5); is not statement so you need to either print the value or return it

Third, while (apple = 0) { is wrong because compiler looking for Boolean expression

while(Boolean_expression)
{
   //Statements
}

change to while (apple == 0 ) {

Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58