Error: 'else' without 'if'
Remove the semicolon at the end of this line:
if (temp > 95 || temp < 20);
And please, please use curly brackets! Java is not like Python, where indenting the code creates a new block scope. Better to play it safe and always use curly brackets - at least until you get some more experience with the language and understand exactly when you can omit them.
Else without if error: I dont understand why java isn't recognizing my if statment
Here's the problem:
if(dDaysWorked <= WEEK); // remove the ;
That trailing ;
is making Java believe that the if
statement is finished, and the {}
block after it is outside the if
condition, consequently the else
part has no matching if
preceding it.
This is a rather frequent bug, and a hard one to spot. If it weren't for the else
block, the code would have compiled correctly, but it would have been wrong. Bottom line: never, ever put a ;
in the opening line of an if
, for
or while
statement.
Java Error: 'else' without 'if'
First of all there is a lot of syntax error and then if else
conditional statement error. I java you need to define a variable, without defining you can not use them.
Secondly you need to learn how to use Class and its methods.
While debugging your code, see what I have got and I have put comment on the codes that your need to write.
import java.util.Random; //Not used in your code, no need to import
public class Dice {
public static void main(String[] args) {
int turn;
turn = (int) (Math.random() * 2 + 1);
int score1 = 0;
int score2 = 0;
int roll1 = 0;
int roll2 = 0;
while (score1 <= 75 || score2 <= 75){
if (turn == 1 & (roll1 != roll2)) {
//roll1 = roll1.random(); //please correct this
//roll2 = roll2.random(); //put correct code that you want
score1 += (roll1 + roll2);
System.out.println("Player 1 rolls a" + roll1 + "and a" + roll2);
turn = 2;
}
else if (roll1 == roll2) {
while (roll1 == roll2) {
System.out.println("Player 1 gets to roll again");
//roll1 = roll1.random();
//roll2 = roll2.random();
score1 += (roll1 + roll2);
System.out.println("Player 1 rolls a" + roll1 + "and a" + roll2);
score1 += (roll1 + roll2);
}
}
else {
if (score1 >= 75) {
System.out.println("Player 1 wins!");
turn = 2;
}
}
if (turn == 2 & roll1 != roll2) {
//roll1 = roll1.random();
//roll2 = roll2.random();
score2 += (roll1 + roll2);
System.out.println("Player 2 rolls a" + roll1 + "and a" + roll2);
turn = 1;
}
else if (roll1 == roll2) {
while (roll1 == roll2);
System.out.println("Player 2 gets to roll again");
score2 += (roll1 + roll2);
//roll1 = roll1.random();
//roll2 = roll2.random();
System.out.println("Player 2 gets to roll again");
}
else {
if (score2 >= 75) {
System.out.println("Player 2 wins!");
turn = 1;
}
}
}
}
}
To get rid of syntax error, you can use IDE like Netbeans, Eclipse and to understand how conditional statement work, please see
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Please Look at JAVA step by step.
This is the link where you can start Tutorials Point
Search some video tutorials about writing JAVA programs.
Android Studio Else without if error
public boolean onTouchEvent(MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
action_flg = true;
} else if (MotionEvent.ACTION_UP == me.getAction()) {
action_flg = false;
return true;
}
}
Related Topics
Differencebetween Persist() and Merge() in JPA and Hibernate
Differencebetween Field, Variable, Attribute, and Property in Java Pojos
Safely Casting Long to Int in Java
What Does Value & 0Xff Do in Java
Difference Between 'Optional.Orelse()' and 'Optional.Orelseget()'
Why Doesn't Mockito Mock Static Methods
How to Mark Jtable Cell Input as Invalid
Java Simpledateformat Always Returning January for Month
Calling One Jframe from Another Using Timer Without Any Buttons
How Would You Implement an Lru Cache in Java
How to Convert a Char Array Back to a String
Running Code After Spring Boot Starts
Volley - Sending a Post Request Using JSONarrayrequest
Using the Keyword "This" in Java
How to Escape % in String.Format
Https Get (Ssl) with Android and Self-Signed Server Certificate