Howdy, Stranger!

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


conditional statements

driftingdrifting Member Posts: 1
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...
if (twenty != 0) // the number of twenties given in change
if (twenty == 1)
System.out.print(twenty + " twenty dollar bill");
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]


  • sunlotussunlotus Member Posts: 55

    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.

    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.