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

时间:2015-11-15 02:14:49

标签: java loops sentinel

我正在寻找一种更紧凑的方法来接近我的哨兵循环。我在这里的代码完全像我想要的那样工作,但我很好奇是否有更干净或更简洁的方法来做这个没有任何额外的循环或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();

2 个答案:

答案 0 :(得分:0)

您可以使用Math.min(int, int)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);

答案 1 :(得分: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();

        }
    }
}
}

}