While loop exiting before computation because of datatypes

泄露秘密 提交于 2019-12-02 15:53:07

问题


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. The sum of the digits is 27. I left out some code of the whole program. It has a clean compile but when it runs it automatically terminates. I think the problem lies somewhere in the conversion of the datatypes.

int randomNumber = rand.nextInt(9000) + 1000;
String randomString;
boolean found = true;   

while (found)
{   

// converting to string to find position of digits and value        
randomString = String.valueOf(randomNumber);

// converting to char to transfer back to in while knowing the position of the digits 
char position0a = randomString.charAt(0);
char position1a = randomString.charAt(1);
char position2a = randomString.charAt(2);
char position3a = randomString.charAt(3);

// coverted back to int
int position0 = Character.getNumericValue(position0a);
int position1 = Character.getNumericValue(position1a);
int position2 = Character.getNumericValue(position2a);
int position3 = Character.getNumericValue(position3a);

int sumCheck = position0a + position1a + position2a + position3a;
int digit30Check = 3 * position2;

//checking addition to 27
String sumCheck27 = "27";
String sumCheck28 = String.valueOf(sumCheck);

// checking all digits are different
if (position0 != position1 && position0 != position2 && position0 != position3  &&

position1 != position2 && position1 != position3 && position2 != position3) 
{
if (position3 != digit30Check) // check if the digit in the thousands place 3 * tens
{
    if (sumCheck27.equals(sumCheck28)) // check if the sum is 27
    {
        if (position0 != 1 && position0 != 3 
&& position0 != 5 && position0 != 7 &&      
position0 != 9 && position1 != 1 && position1 != 3 
&& position1 != 5 && position1 != 7 && 
position1 != 9 && position2 != 2 && position2 != 3 
&& position2 != 5 && position2 != 7 && 
position2 != 9 && position3 != 3 && position3 != 3 && 
position3 != 5 && position3 != 7 && position3 != 9)
        { 
// checks for odd digits
         found = false;
         System.out.println(randomNumber);

        }
        else 
        randomNumber = rand.nextInt(9000) + 1000;
    }
    else 
    randomNumber = rand.nextInt(9000) + 1000;               
}
else 
randomNumber = rand.nextInt(9000) + 1000; 
}
else 
 randomNumber = rand.nextInt(9000) + 1000; 

 // end while
 }

回答1:


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



回答2:


Swap while(found) for while(!found).

Just think about it logically. You don't want to look while you've found something. You want to keep looking while you've not found it.




回答3:


I believe this can be simplified significantly by looking at the given inequalities, soemthing like

// thousands + hundreds + tens + ones == 27
// thousands = tens * 3, or tens = thousands / 3
// thousands = 3, 6 or 9
public static void main(String[] args) {
    // thousands = tens * 3; so it must be a multiple of 3.
    for (int thousands = 3; thousands < 10; thousands += 3) {
        for (int hundreds = 0; hundreds < 10; hundreds++) {
            if (hundreds == thousands) {
                continue;
            }
            // thousands = tens * 3; so the reverse is also true
            int tens = thousands / 3;
            if (tens == hundreds) {
                continue;
            }
            // All of the digits sum to 27.
            int ones = 27 - thousands - hundreds - tens;
            if (ones > 9 || ones % 2 != 0 || ones == tens
                    || ones == hundreds || ones == thousands) {
                continue;
            }
            int val = (thousands * 1000) + (hundreds * 100) + (tens * 10)
                    + ones;
            System.out.println(val);
        }
    }
}

Of course, the output is still

9738


来源:https://stackoverflow.com/questions/26556048/while-loop-exiting-before-computation-because-of-datatypes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!