geletine said:
I have had a go at the last task and i confident i am progressing, but
i am not sure quite there yet
here is the code
import java.io.*;
public class odd
{
public static void main(String args[] )throws IOException
{
int number,oddnum,evenum,sumeven,sumodd;
String inData;
BufferedReader stgin = new BufferedReader ( new InputStreamReader
(System.in ) );
number = 0;
evenum = 0;
oddnum = 0;
sumeven = 0;
sumodd = 0;
if while (number%2 == 0)
{
System.out.println ("Enter a number please?")
inData = stgin.readLine()
number = Integer.parseInt ( inData )
number = evenum;
sumeven = sumeven + evenum;
{
else
{
System.out.println ("Enter a number please?")
inData = stgin.readLine()
number = Integer.parseInt ( inData )
number = oddnum;
sumodd = sumodd + oddnum;
}
System.out.println ( sumeven);
System.out.println( sumodd);
}
}
I just have two somewhat long comments:
(1) I didn't see your revised pseudocode. Maybe you posted it and it
never reached my newsserver, in which case just let me know and I'll use
GoogleGroups or something to find it. Otherwise, it's not clear that you've
a fully devised a plan for solving this problem. If you don't have a plan,
then you're just typing blindly, and there's no real reason to believe that
the code you write down would solve the problem you're trying to solve. You
should be confident that your plan will work before you start typing in
code, and one way to do that is to write out pseudo code and go through it
and check that the pseudocode fills all the requirements of the problem.
(2) In my personal opinion, if you're confused or kind of lost in your
program, it's better to give your variable names which are too long, as
opposed to names which are too short. Giving your variables highly
descriptive names -- that reveal what their contents are and their
purpose -- will make it a lot easier to spot mistakes in the logic of your
code. As you get more comfortable programming, you get lazier; since it
takes a long time to type in a long name, you'll start to shorten the names.
But when you want clarity, go for longer names.
For example, you have a variable called "number", which is somewhat
vague. A person's age happens to be a number, so does this variable contain
someone's age? Perhaps it contains someone's social security number? Or does
it contain the number of times something should happen? Or something else?
Here, it looks like you're using that variable to store the number the
user entered in. If that's the case, you might name the variable
"numberThatTheUserEnteredIn". That's a bit better, but even then, it's not
clear what is the meaning of the number that the user entered in.
Let's say your writing some sort of penguin simulation game, SimPenguin,
and you prompt the user "How many penguins would you like to simulate?" In
this case, a better name than either "number" or
"numberThatTheUserEnteredIn" is "numberOfPenguinsToSimulate". This new name,
"numberOfPenguinsToSimulate", gives a clear indication of what kind of value
is stored in the variable, and what it should be used for.
- Oliver