[java] nested if/else statement problems
Ok i'm having a little trouble with nested if/else statements, I get an error saying 'else' without 'if' at line 71, here is my code. It has to be in a similar format that I have to get full credit.
Code:
package lab3; //if statement to figure out how much comission is owed from sales if (totalsales < 100 && totalsales > 1.00); { { comission = totalsales * .05; } else if { (totalsales >= 100 && totalsales < 300); { totalsales * .10 = comission; } else (totalsales >=300) comission = totalsales * .15; } } |
When posting crapload of code like that, format it properly using [ code ] tags
You don't need a ; at the end of the if statement line if (hoursworked > standardhours);<--- Code:
if (totalsales < 100 && totalsales > 1.00); { Code:
if(something=0) |
Code:
if (totalsales < 100 && totalsales > 1.00); <------ error 1 What you probably want is: Code:
if (totalsales < 100 && totalsales > 1.00) If you want to use braces, you shouldn't need to however, an if statement without braces automatically includes the next line (in this case an if statement such that we have multiple lines), this would be wrong: Code:
if (some condition) Code:
if (some condition) Code:
if (totalsales < 100 && totalsales > 1.00) |
Go get a text editor with syntax highlighting.
|
this has nothing to do with if/else statements. it has to do with not knowing the syntax of the language.
If you look at your very first 'if' statement where you were calculating overtime hours, you got that wrong too. |
Quote:
Code:
package lab3; |
Much better, always keep your braces consistant, mixing can get confusing, (also normally loses you "style" marks on practicals), also remember to pick a style of if and stick with it:
Method 1: Code:
if (condition){ Code:
if (condition) Final thing would be comments, while nothing is wrong with your current style it does make the code harder to read where I work we always keep to 1tab beyond the longest line in a section of code: Code:
statement1; // Consistant comment placement Code:
public class Lab3 Final point is variable naming, when using multiple words (you have descriptive names, thats good, too often you see nothing but a, b and d, you should really capitalise or split the words, standardHours,standard_hours or StandardHours is more readable than standardhours and the cost is virtually none in terms of space/effort. Sorry for lecturing you, both parents are teachers as is my brother and my dad was a programmer for years... programming discussions round the table happen often! |
All times are GMT -8. The time now is 06:29 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project