Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

conditional statements

i am trying to display some inputted data with grammatical correctness, but am having trouble programming this without an enormous amount of code. I want to tell it to print an 'and' before the last value in a sequence. But in a series of 8 values that are only printed if it doesn't equal 0 i can't figure out how to tell it this. whether in a single command or a clever series. please give suggestions. here's my code in case that helps...
[code]
if (twenty != 0) // the number of twenties given in change
if (twenty == 1)
System.out.print(twenty + " twenty dollar bill");
else
System.out.print(twenty + " twenty dollar bills");
if (ten != 0 || five != 0 || one != 0 || quarter != 0 || dime != 0 || nickel != 0 || penny != 0)// if these aren't zero then it's a sequence that requires a comma
System.out.print(", "); // what if only one variable follows? i do i tell it to instead print an 'and'? [/code]

Comments

  • sunlotussunlotus Posts: 55Member

    I'm not sure what is the requirement. I guess you receive a variable number of elements that you have to print with a comma and type an "end" before the last element. Or maybe I am somewhere close to your requirement. My suggestion is declare an array, and add each element that you read.

    Outputting
    if(array_length>=3){
    //loop over the array till the penultimate element inclusive
    for(int i=0; i<array_length-1; i++)
    print array[i].",";
    }
    print " and". array[last_element_position];

    As for the case that the array has 1 or 2 elements, deal with them separately.

Sign In or Register to comment.