week 1 exercise 18 Grades and Points - writing a totally different code then expected

503 views
Skip to first unread message

mallet

unread,
Aug 18, 2016, 7:28:38 AM8/18/16
to mooc.fi
Hi all

I'm having problems with week 1 exercise 18  on course Grades and Points.

So the problem I having is understanding what code needs to be implemented. I understand what the question is asking me to do but, I dont what code I should implement.

Reading from other posts about this exercise, it looks like the starting code should be something like this:
 if (num1 >= 0 && num1 <= 20) {
            System.out.println("Failed");

But I have done something totally different:


import java.util.Scanner;

public class GradesAndPoints {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        System.out.print("Type the points [0-60]:");
        int number = Integer.parseInt(reader.nextLine());

        if (number == 29) {
            System.out.println("Failed");
        } else if (number == 34) {
            System.out.println("Grade: 1");
        } else if (number == 39) {
            System.out.println("Grade: 2");
        } else if (number == 44) {
            System.out.println("Grade: 3");
        } else if (number == 49) {
            System.out.println("Grade: 4");
        } else if (number == 60) {
            System.out.println("Grade: 5");
        }

    }
}



Its very frustrating for a newbie like me to overcome problems like this even at the beginning of this course. but I really want to understand how to code. Was there an example prior to this that explains how to compare with the && operators or other tips that can help me understand this a bit better?

ljleppan

unread,
Aug 19, 2016, 8:41:48 AM8/19/16
to mooc.fi
Hi,

I'd suggest you re-read some of the material starting from the heading "8. CONDITIONAL STATEMENTS AND TRUTH VALUES" to see how to use the less-than, less-than-or-equal etc. types of comparisons. Right now, your code is printing (for example) "Failed" if and only if the number is exactly 29. So for example 28 wouldn't print anything.

To make your program print "Failed" for all number less than or equal to 29, you would instead use "if (number <= 29) {". This is read as "if number is less than or equal to 29".

You can also correctly do the rest of the grades without any "&&" operators. 

Consider (and try out!) the following code:

int a = 1;
if (a < 2) {
 
System.out.println("less than two");
} else if (a < 3) {
 
System.out.println("less than three");
}
System.out.println("All done!");

Can you figure out why "less than three" does not get printed? The key is in the "else" keyword. Check what that does. What happens if you remove the "else" from the code? What happens if you change "int a = 1;" to "int a = 2;"? The sections 8.2 and 8.3 of the material talk about if and else if. Read those again if you are struggling to understand what is happening :)

Also, logical operations like && and || are not required for that exercise and will be covered soon after it in the material, don't worry about those just yet.

If you still have questions after playing around with the example code I gave you, please ask for clarification :)

-L

mallet

unread,
Aug 22, 2016, 10:25:55 AM8/22/16
to mooc.fi
Thank you Leo. Thats helped me a lot and I solved my answer. Thanks very much for taking your time out to help answer my question =)
Reply all
Reply to author
Forward
0 new messages