How can I remove while (true) from my loop in Java?

I heard that using while (true) is a bad programming practice.

So, I wrote the following code to get some numbers from the user (with default values). However, if the user is of type -1, then he will exit the program for them.

How should this be written then without any time (true)? Can I come up with a condition to make the while loop go off, which will be caught right away without continuing until the next iteration?

Here's how I have it now:

 public static void main(String[] args)
    {
        System.out.println("QuickSelect!");

        while (true)
        {
            System.out.println("Enter \"-1\" to quit.");

            int arraySize = 10;
            System.out.print("Enter the size of the array (10): ");
            String line = input.nextLine();
            if (line.matches("\\d+"))
            {
                arraySize = Integer.valueOf(line);
            }

            if (arraySize == -1) break;

            int k = 1;
            System.out.print("Enter the kth smallest element you desire (1): ");
            line = input.nextLine();
            if (line.matches("\\d+"))
            {
                k = Integer.valueOf(k);
            }

            if (k == -1) break;

            List<Integer> randomData = generateRandomData(arraySize, 1, 100);

            quickSelect(randomData, k);
        }
    }
+3
source share
8 answers

while (true)fine. Keep it.

, , , , , while (true) .

+10

(SESE), , break, continue , ). , , , . . , , ( ).

public static void main(String[] args) {
    ...
    do {
        ...
        if (arraySize == -1)  {
            ...
            if (k != -1) {
                ...
            }
        }
    } while (arraySze == -1 || k == -1);
    ...
}

, , , (!) , "-", , .

+3
    bool exit = false;
while (!exit) {
    ...
    ...
    if (k == -1) {
        exit = true;            
    }
    else {         
        List <Integer> ....;
        quickselect(.......);
    }
}

, , while . if .

+2

, , , :

bool complete = false;

while (!complete)
{

    if (arraySize == -1)
    {
        complete = true;
        break;
    }
}

, , , , , 100 , , , . , . " , .. ...", "complete" true, . , .

+1

while(true), for(;;). , .

0

while (true) , " "!

, "q" : "while (! line.equals(" q "))".

0

, , , .

, k , :

    for (int arraySize; ( arraySize = readArraySize ( input ) ) != -1;) {
        final int k = readKthSmallestElement ( input );

        List<Integer> randomData = generateRandomData(arraySize, 1, 100);

        quickSelect(randomData, k);
    }

. != -1 arraySize, k randomData , , QuickSelect null, :

    for ( QuickSelect select; ( select = readQuickSelect ( input ) ) != null; ) {
        select.generateRandomData();
        select.quickSelect();
    }        

, QuickSelect , :

    for ( QuickSelect select : new QuickSelectReader ( input ) ) {
        select.generateRandomData();
        select.quickSelect();
    }        

QuickSelectReader Iterable, QuickSelect, arraySize, k, . , .

, ; , main() .

, "-1" "\\d+", .

0

while (true) {} . , 1000- , , ....

0

Source: https://habr.com/ru/post/1719193/


All Articles