每次用户输入数字时如何防止程序要求使用输入数字?

时间:2014-11-10 01:08:52

标签: java java.util.scanner

import java.util.Scanner;

public class AverageMaxMin 
{
    @SuppressWarnings("resource")
    public static void main(String[] arg)
    {
        int count = 0;
        double sum = 0;
        double maxium = 0;
        double minium = 100;

        Scanner kb = new Scanner(System.in);

        System.out.println("Please enter a value between 0 and 100, or -1 to quit: ");
        double input = kb.nextDouble();

         while (input != -1)
            {

             while ((input > 100) || (input <= -1))
              {
                 System.out.println(" Invalid! Please enter a value between 0 and 100, -1 to show the result: ");
                 input = kb.nextDouble();
              }

                count++;             
                sum += input; 

                if (maxium < input)
                {
                    maxium = input;
                }

                if (minium > input)
                {
                    minium = input;
                }


                System.out.println("Please enter a value between 0 and 100, or -1 to quit: ");
                input = kb.nextDouble();

            }
                double average = (sum / count);  
                System.out.println("The average is: " + average);
                System.out.println("The maxium is: " + maxium);
                System.out.println("The minium is: " + minium);

    }

}

当我运行代码时,当用户输入一个数字,并想要输入另一个数字时,每次都会弹出“请输入0到100之间的值,或者输入-1以退出:”。

例如: 请输入介于0和100之间的值,或输入-1以退出: 100 请输入介于0和100之间的值,或输入-1以退出: 0 请输入介于0和100之间的值,或输入-1以退出: -1 平均值是:50.0 最大值是:100.0 min是:0.0

我希望能够让用户输入一个输入列表而不输入“请输入0到100之间的值,或者输入-1以退出:”每次都会弹出。

例:
100 0 -1 平均值是:50.0 最大值是:100.0 min是:0.0

请帮帮我。 编程新手

2 个答案:

答案 0 :(得分:2)

更改循环样式以使用do-while循环而不是while-loop

基本上,您希望至少进入一次循环周期,例如......

import java.util.Scanner;

public class Test {

    @SuppressWarnings("resource")
    public static void main(String[] arg) {
        int count = 0;
        double sum = 0;
        double maxium = 0;
        double minium = 100;

        Scanner kb = new Scanner(System.in);

        double input = -1;
        // Main processing loop...
        do {

            // Validation loop...
            do {
                System.out.println("Please enter a value between 0 and 100, or -1 to quit: ");
                input = kb.nextDouble();
                kb.nextLine();
            } while (input > 100);

            if (input > -1) {
                count++;
                sum += input;

                if (maxium < input) {
                    maxium = input;
                }

                if (minium > input) {
                    minium = input;
                }
            }
        } while (input != -1);

        double average = (sum / count);

        System.out.println(
                        "The average is: " + average);
        System.out.println(
                        "The maxium is: " + maxium);
        System.out.println(
                        "The minium is: " + minium);

    }

}

这确实使用了两个循环,您可以将其减少为一个并忽略任何超出范围的值,例如......

import java.util.Scanner;

public class Test {

    @SuppressWarnings("resource")
    public static void main(String[] arg) {
        int count = 0;
        double sum = 0;
        double maxium = 0;
        double minium = 100;

        Scanner kb = new Scanner(System.in);

        double input = -1;
        do {

            System.out.println("Please enter a value between 0 and 100, or -1 to quit: ");
            input = kb.nextDouble();
            kb.nextLine();

            if (input > -1 && input <= 100) {
                count++;
                sum += input;

                if (maxium < input) {
                    maxium = input;
                }

                if (minium > input) {
                    minium = input;
                }
            }
        } while (input != -1);

        double average = (sum / count);

        System.out.println(
                        "The average is: " + average);
        System.out.println(
                        "The maxium is: " + maxium);
        System.out.println(
                        "The minium is: " + minium);

    }

}

答案 1 :(得分:0)

你只需要删除在第一个while循环中打印该消息的行,在它的末尾,就在输入之前。