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);
}
}
Samatha84
source
share