Java 哨兵循环,更简洁或更有条理的方式来做到这一点?

Java sentinel loop, cleaner or more organized way to do this?

我正在寻找一种更紧凑的方法来处理我的哨兵循环。我这里的代码完全按照我的预期工作,但我很好奇是否有更干净或更整洁的方法来执行此操作而无需任何额外的循环或 if 语句。谢谢!

public void findSmallLarge()
{
    //declaring variable
    int maxValue = 0;
    int minValue = 0;
    int input = 0;
    boolean done = false;
    String sentinel;

    //Scanner object
    Scanner in = new Scanner(System.in);

    //loop asks user to input integers and will tell the user the minimum and maximum values, after the program quits.
    System.out.println("Please enter an integer, or press Q to quit");
    while (!done && in.hasNextInt())
    {           
        System.out.printf("Please enter an integer, or press Q to quit: ");
        input = in.nextInt();

        // if input is greater than the maxvalue, update the maxvalue.
        if (input > maxValue)
            {
            maxValue = input;
            }

        // if input is less than the minimum value, update the minimum value.
        if (input < minValue)
        {
            minValue = input;
        }

        // if the next input is not an int AND done is false, run the loop until an integer is entered.  If q/Q is entered, quit the program.
        if (!in.hasNextInt() && !done)
        {
        while (!in.hasNextInt() && !done)
        {
            sentinel = in.next();
            System.out.print("Please enter a valid integer, or press Q to quit: ");
            if (sentinel.charAt(0) == 'Q' || sentinel.charAt(0) == 'q')
            {
                System.out.println();
                System.out.println("You have quit the program");
                done = true;
                break;
            }
        }
        }
    }
    // Print the updated min and max values
    System.out.println("The Max value is: " + maxValue);
    System.out.println("The Minimum value is: " + minValue);
    System.out.println();

您可以使用 Math.min(int, int) and Math.max(int, int) 来替换您的 input > maxValueinput < minValue 支票,例如

maxValue = Math.max(maxValue, input);
minValue = Math.min(minValue, input);

你应该像这样声明它们

int maxValue = Integer.MIN_VALUE;
int minValue = Integer.MAX_VALUE;

至于你的 sentinel 循环,你可以使用

 if (Character.toUpperCase(sentinel.charAt(0)) == 'Q')

你也可以删除 done 布尔值,然后使用

System.exit(0);

我找到了我正在寻找的确切答案。我在上面发布的代码不断要求用户输入,当输入错误(即除整数以外的任何东西)时,它会告诉用户他们的输入是错误的,当用户按下 Q 或 q 时,程序结束。我找到了我正在寻找的更直接的版本。

import java.util.Scanner;

public class NovLecture {

public static void main(String[] args) {



Scanner in = new Scanner(System.in);
boolean done = false;
System.out.println("Please enter an integer or press Q to quit");

// While done is not false, run this loop
while(!done)
{
    // if the next input is an int, do this, other wise continue to else
    if(in.hasNextInt())
    {
        int input = in.nextInt();

    } 
    // If input is anything other than an int, place the input into a string
    else 
    {
        String input =  in.next();

        // and if the input is equal to a q or Q, end the program
        // by setting done to true
        if (input.charAt(0) == 'q' || input.charAt(0) == 'Q')
        {
            done = true;
        } 

        //otherwise tell the user the input is bad and to try again, and flush the input.
        else
        {
                System.out.println("Bad input, please try again");
                in.next();

        }
    }
}
}

}