【问题标题】:Java sentinel loop, cleaner or more organized way to do this?Java哨兵循环,更清洁或更有组织的方式来做到这一点?
【发布时间】:2015-11-18 02:10:52
【问题描述】:

我正在寻找一种更紧凑的方法来处理我的哨兵循环。我在这里的代码完全按照我的意图工作,但我很好奇是否有一种更简洁或更简洁的方法来做到这一点,而无需任何额外的循环或 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();

【问题讨论】:

    标签: java loops sentinel


    【解决方案1】:

    您可以使用 Math.min(int, int)Math.max(int, int) 替换您的 input &gt; maxValueinput &lt; 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);
    

    【讨论】:

    • 哈哈,我很好奇如何在其中实现数学方法,但我主要想知道我的哨兵循环。有没有更紧凑的方式来编写它,或者我的方式是否正确/足够?例如,将布尔值与!in.hasNextInt() 一起使用。
    • 啊,非常酷,感谢您的意见。我会玩弄它,看看它们是如何工作的 :) 谢谢!
    【解决方案2】:

    我找到了我正在寻找的确切答案。我在上面发布的代码不断地询问用户输入,当输入错误(即不是整数)时,它会告诉用户他们的输入是错误的,当用户按下 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();
    
            }
        }
    }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 2012-02-12
      • 2010-10-26
      • 2010-12-08
      相关资源
      最近更新 更多