Play around with scales. Scales are runs of notes that can be combined to achieve a certain sound. Play notes from scales, always centering on the "first," when coming up with a melody. Play the notes C, D, E, F, G, A and B to play in the C major scale. Use the first (or the "root") in conjunction with the fourth or fifth to come up with melodies that sound nice together. Notes in scales are numbered to aid understanding of the relationships between notes in scales. In C major, C is the first, D is the second, E is the third and so on. Use major scales for happy sounding melodies, and minor scales for sad sounding melodies. An easy minor scale is A minor, which includes the notes A, B, C, D, E, F and G. Note how the notes themselves are the same as the C major scale; this is because A minor is the "related" minor to C, meaning you can use the A minor scale to add sad portions to happy songs in the key of C.
Include some repetition in your melody. Melodies with no repetition can be hard to remember, and will not stick in your listeners' minds. Come up with a short run of notes that sounds nice, and use that as a repeating phrase throughout the melody. Try not to use it too much, as this can easily sound boring. Play variations on the theme, add in extra chromatic notes or play some parts quieter or louder to add variety. Chromatic notes do not fit into the scale, but can be played in "passing" to give the melody a different sound or feel.
Think about the "shape" of your melody. Try to write your melody so it goes both up and down in pitch. Playing melodies that go up and down on the staff instead of just up or down helps the music flow smoothly. Melodies in which the notes only rise or fall in pitch can sound like scales rather than melodies. Try to contain the melody within a single octave, but use this only as a general guideline.
Stop your melody at the right time. Melodies should generally last around four bars, but can be longer or shorter on occasion. Don't let your melody go on too long, as it could easily become hard to listen to. Keep your melody between one and eight bars for the best effect. If you are writing a melody for a vocal or to go other an existing chord pattern, learn when the chordal changes are, and keep your melody to the same length.
Discard the rules when it feels right. Think of the methods listed above as guidelines. Music is a creative pursuit, and if everybody stuck to the same rules it would become uninteresting and repetitive. If you like the sound of something that doesn't necessarily fit within the guidelines, don't worry about it. Keep it the way it is. Learning when to ignore the rules is as important as learning when to follow them.