While loop exiting before computation because of datatypes

后端 未结 3 1340
遇见更好的自我
遇见更好的自我 2021-01-28 09:37

My program specifications are as follows. 1. All four digits are different 2. The digit in the thousands place is three times the digit in the tens place 3. The number is odd. 4

3条回答
  •  [愿得一人]
    2021-01-28 10:33

    boolean found = false;  
    
    while (found)
    

    This alone ensures the while loop will never be entered, since found is false. Anything inside the while loop doesn't make any difference, since it will never be executed.

    You probably meant to write

    while (!found)
    

    Beside this error, your conditions are over complicated. Here's how you can simplify them :

    if ((position0 == (3 * position2)) && // note that position0 is the "thousands place", not position3
        ((position0+position1+position2+position3) == 27) && // sum of digits
        (position3 % 2 == 1) && // odd number
        (position0 != position1 && position0 != position2 && position0 != position3  &&
         position1 != position2 && position1 != position3 && position2 != position3)) { // different digits
        found = true;
    }
    

提交回复
热议问题