Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Using Ternary Operator and Boolean

anne17821anne17821 Member Posts: 7
Hi,

I am trying to learn Java programming. A book I am reading asks that I create a program that will sum only the odd numbers from 1 to 1000. I am to use a Boolean variable and the ternary operator.

I have read a bit and researched the ternary operator, but am at a loss on how to use a boolean variable to get this information.

Can anyone give a suggestion?
Thanks,

Comments

  • arb123arb123 Member Posts: 696
    : Hi,
    :
    : I am trying to learn Java programming. A book I am reading asks that I create a program that will sum only the odd numbers from 1 to 1000. I am to use a Boolean variable and the ternary operator.
    :
    : I have read a bit and researched the ternary operator, but am at a loss on how to use a boolean variable to get this information.
    :
    : Can anyone give a suggestion?
    : Thanks,
    :

    I guess it means something like this:
    [code]
    boolean isEven = (i % 2 == 0);
    total += isEven ? 0 : i;
    [/code]
    This could be folded into one expression.

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • anne17821anne17821 Member Posts: 7
    : : Hi,
    : :
    : : I am trying to learn Java programming. A book I am reading asks that I create a program that will sum only the odd numbers from 1 to 1000. I am to use a Boolean variable and the ternary operator.
    : :
    : : I have read a bit and researched the ternary operator, but am at a loss on how to use a boolean variable to get this information.
    : :
    : : Can anyone give a suggestion?
    : : Thanks,
    : :
    :
    : I guess it means something like this:
    : [code]
    : boolean isEven = (i % 2 == 0);
    : total += isEven ? 0 : i;
    : [/code]
    : This could be folded into one expression.
    :
    : ---------------------------------
    : [size=1](Its just my sig)
    : HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]
    :
    :
    Thank you. I will try that suggestion to see if it works as expected. By the way, is the word isEven a built-in method or something like that in Java? If so, can anyone tell me how I can find out what such built-in methods, keywords, classes are for Java?

    I am used to VBA programming. In that language if there is a keyword like isEven it would be listed in the help file so you could find it. I looked in the index of the books I have on Java (one of them is called "Java J2SE: The Complete Reference") and can find no listing for isEven. Also, in VBA, when you type the period, you get a listing of methods or events that can be used. I am using a program called JEdit to create my Java programs, but this program gives no help at all on the keywords and such that I can use. Can anyone suggest a better way to create Java programs where I will actually get some help on the syntax of the Java language?

    Thanks so much,
    Anne
  • arb123arb123 Member Posts: 696
    [blue] By the way, is the word isEven a built-in method or something like that [/blue]

    The modulus operator % gives the remainder of a division operation. If the remainder is 1 when divided by 2, then the integer is odd; otherwise it must be even.
    [code]
    public static boolean isEven(int n) {
    return n % 2 == 0;
    }
    [/code]

    I guess you could do it more efficiently using bitwise ops, but this is simpler to read and understand.

    The javadoc libraries (on the http://java.sun.com site) are the documentation for the standard API calls. Language syntax references are also available. I recommend you follow some of the many tutorials on that site aswell.

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

Sign In or Register to comment.