While statement

C

Chris Smith

zero said:
In programming there are no absolutes of course. If I say "never", I
actually mean "never unless you are an experienced programmer and have a
compelling reason".

I agree with Oliver that this is the wrong default in this case. When
writing code that needs to conform to a specified use of stdout and
stderr, you might need to catch exceptions in main. Similarly, if you
have an error reporting infrastructure such as a logging package that
sends email to the software development team, then you have a good
reason to catch exceptions (and, likely, to rethrow them when you're
done). Otherwise, just let them be thrown.
In general though, I think there is little use in main throwing an
exception.

.... except that it's an easy way to be sure that a complete error report
is available when it occurs, without doing it yourself.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

Tom Leylan

geletine said:
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

Hey how's things? Let me give you the big picture... I'm big on painting
the big picture before working on the details.

Consider the pseudocode you're written already converted to Java, what do
you have? You have a program that asks for input, compares a value, sums
the numbers and prints the values. Frankly you will never need that program
again. :)

Consider instead what you would have if you had a "NumberInfo" class that
had methods that corresponded to all sorts of things you might need to
compute. So you would provide a number (probably input from somewhere but
the method doesn't care) and it would return say... sumEvenValues() and
sumOddValues(). Notice these methods wouldn't output them it would just
compute them and return them. Then you could create a program that asks for
input, reads input from a file or has a value hardcoded into it and it could
output the value to the screen, to a printer or to another file.

And here is the big plus... when you need computeAverage() you would simply
add that method. This wouldn't destroy or in any way alter your current
program but it would provide another functionality which somebody (perhaps
you) could use at some point. The system could "grow."

In short, break a problem up into little problems, program solutions to
those little problems and assemble those solutions into one that satisfies
your original problem.

Hope this helps,
Tom
 
G

geletine

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);

}
}
 
O

Oliver Wong

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
 
R

Roedy Green

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;
}

note how there are 3 lines the same in both branches of the if? Factor
that out of the if so it appears only once.

Also you want to do your even-odd test on the most recently-entered
number, not the previous one.
 
G

geletine

A collegue helped me complete the program.
I hope its clear.

Here is the code and a sample of it working

import java.io.*;
public class odd
{
public static void main(String args[] )throws IOException
{

int number,sumeven,sumodd,storage;
String inData,evenum,oddnum;
BufferedReader stgin = new BufferedReader ( new InputStreamReader
(System.in
) );
sumeven = 0;
sumodd = 0;
evenum = " ";
oddnum = " ";
storage = 0;

System.out.println ("Enter a number please?");
inData = stgin.readLine();
number = Integer.parseInt ( inData );


while (storage <= number)
{
if ( storage %2 == 0)
{
sumeven = sumeven + storage;
evenum=evenum + Integer.toString (storage) + " ";
}

else
{
sumodd = sumodd + storage;
oddnum = oddnum+Integer.toString (storage) + " ";
}
storage = storage + 1;
}

System.out.println (" Sum of even is " + sumeven );
System.out.println (" Sum of odd is "+ sumodd );
System.out.println (" odd numbers are " + oddnum);
System.out.println (" even numbers are " + evenum);
}
}

allix@allix:~/javafiles$ java odd
Enter a number please?
10
Sum of even is 30
Sum of odd is 25
odd numbers are 1 3 5 7 9
even numbers are 0 2 4 6 8 10
allix@allix:~/javafiles$ java odd
Enter a number please?
25
Sum of even is 156
Sum of odd is 169
odd numbers are 1 3 5 7 9 11 13 15 17 19 21 23 25
even numbers are 0 2 4 6 8 10 12 14 16 18 20 22 24
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top