geletine said:
I am not totally confident with do loops at the moment.
do they allow mutliple choice?
I know about the if ,else and case selection construction, its just i
have problems finding a solution to the following:-
the program must ask the user for a integer value and print the sum of
all even between 1 and the imputed value inclusive, as as well as the
sum off all the odd numbers. lastly the program should print the
inputed value some of the eben integers and sum of odd integers.
Your have a lot of spelling mistakes, but you seem to have a strong
grasp of grammar and phrase structure, so it seems to me that you're a
native English speaker, but just not too careful when you type.
When programming, you have to be a very careful. The tiniest typo in a
program will usually completely alter that program's behaviour. I mention
this because when I read your last sentence, I was rather confused: "lastly
the program should print the inputed value some of the eben integers and sum
of odd integers." I could guess that "eben" meant "even", but it took me a
long time to realize what you meant by "print some of the even integers".
I'm guessing you mean "sum", not "some".
See, some people might not have caught that, and then have been unable
to help you out. Anyway, just pointing that out. Now on to the real meat of
the problem...
here is my pseudocode
read users imputed integer numer
start loop
find wheather number is even
if (even)
find out all the odd and even numbers and find the sum off odd and even
numbers
print the number entered, that its even , the odd and even numbers and
the sum of them
else
number is odd and also find odd and even numbers and find the sum off
odd and even numbers
print the number entered, that its odd , the odd and even numbers and
the sum of them
end loop
It sounds like your pseudocode will more or less actually do what the
program is supposed to do. There's one big structural issue, though. You
mention "start loop" and "end loop", but you don't say what the conditions
are for continuing to loop, or when to break out of the loop. Does the
program just keep on doing the above forever, or what?
Also note that you have duplicate code in both branches of your if
statement. When you have a structure like this:
<pseudocode>
if (some condition) {
do A;
do B;
} else {
do A;
do C;
}
</pseudocode>
You can usually[*] extract the common code out, like this:
<pseudocode>
do A;
if (some condition) {
do B;
} else {
do C;
}
</pseudocode>
My other comment is that your pseudocode is very high level. It doesn't
say *how* you plan on finding all the even numbers, or getting the sum of
them, for example.
and lastly here is my attempt of the java code
import java.io.*;
public class odd
{
public static void main(String args[] )throws IOException
{
int number,oddnum,evennum,sumeven,sumodd;
String inData;
BufferedReader stgin = new BufferedReader ( new InputStreamReader
(System.in ) )
;
evenum = 0;
oddnumber = 0;
System.out.println ("Enter a number please?");
inData = stgin.readLine();
number = Integer.parseInt ( inData );
while if (number%2 == 0)// too test wheather number is even or odd
sumeven = number++;
{
number = evenumber + Integer.toString(sumeven)+ " ";
}
System.out.println(" The even numbers " + sumeven);
else
sumodd = number++;
{
number = oddnumber + Integer.toString(sumodd)+ " ";
System.out.println("the odd numbers are " + sumodd);
}
}
}
the code does not work, perhaps my pseudocode is completly wrong to
start off with all my java code is not corisponding to what the
psedocode is.
Yeah, there's a lot of problems with this code. But rather than fixing
the code directly, you should probably go back to the pseudocode and address
the issues I mentioned above; that is: (1) Specify what the condition is for
the loop (2) Add more low-level detail.
- Oliver
[*] I say "usually", because one situation where it could fail is if testing
the condition might have side-effects that affect the subroutine "do A". You
should generally write your if-statements so that the conditions do NOT
cause any side effects, so this should be a very rare exceptional case.