Yep, that's right. There's only one bit of syntax error. You forgot to declare and initialize points to 0.
In terms of performance, my solution is better ( O(1) vs. O(n) ) :P
Type: Posts; User: helloworld922
Yep, that's right. There's only one bit of syntax error. You forgot to declare and initialize points to 0.
In terms of performance, my solution is better ( O(1) vs. O(n) ) :P
it's because of variable scope. I don't know a good way to explain it, but basically you're declaring the int inside the if statement, so nothing outside of it can see it.
The last problem I see...
if lectures <= 5
points = 2 * lectures
else
points = 5 + lectures
an if/else statement only does one or the other... not both. Also, you want to add the 5 points if the lectures is greater than 5.
Yes, you can do points = points+points, but it's not helpful here.
Mmm, there's a few problems but they can be easily fixed.
You don't want to add 1 or 2 to the lecturesAttended to get the points, but rather multiply. You also have it backwards. It should be 2...
if/else statements are really simple. You ask is something true? If it is, you execute one set of code. If it's not, you execute the else code. It's almost exactly like the language equivalent: if...
:( what do you know how to do? From the way you phrased the question, it seems like you really do need some way to test conditionals, and if/else are the simplest.
if/else is basically like it...
what is the algorithm for n lectures attended?
first 5 give 2 points, all rest give 1?
pseudo-code:
count = lecturesAttended
while count > 0
if count > 5
increase points by...
Write your algorithm out for if he attends n lectures. If it's always going to be 7 lectures, you might as well do this:
System.out.println("For 7 lectures you get 12 points");
Even if you're...