如何检查扫描的整数是否确实是整数?

时间:2019-01-25 20:37:08

标签: java

我对Java很陌生,我在C#中编程,这非常相似,但是在用Java捍卫我的代码时遇到了一些麻烦。

我想保护我的代码免受字符串,字符,特殊字符等非法输入的侵害。

我每次都希望在do-while循环中输入除int以外的其他内容来保持住宿(标有?????????的确切位置)

这是我的代码,非常简单,它接受数字并计算最小数量,最大数量,平均数量和总和。 如果输入数字-1,则程序将不会启动,或者循环将停止并打印计算结果。

在此先感谢您的帮助!

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    //Variables
    int count=0;
    int sum=0;
    int max=0;
    int min=0;

    //Initializing minimum value for a reference
    System.out.print("Enter a number ");
    int number = scan.nextInt();
    if(number!=-1){
        min=number;
        count++;
        sum+=number;
    }

            while(number!=-1){   

        do {
            System.out.print("Enter a number or -1 to stop: ");
            number = scan.nextInt();

        }while((Integer.toString(number)==null)||???????????? (Check for illegal input));

        if(number==-1){
            break;
        }
        else {  
            sum+=number;
            count++;
            if(number>max) {
                max=number;
            }
            else if(number<min) {
                min=number;
            }
        }
    }

    if(count!=0)
    {
        System.out.println("The Maximum number is: "+max);
        System.out.println("The Minimum number is: "+min);
        System.out.println("The Sum of all entered numbers is: "+sum);
        System.out.println("The Average of all entered numbers is: "+(double)sum/(double)count);
        System.out.println("T H E    E N D");
    }


    else {
        System.out.println("T H E    E N D");
    }


    scan.close();

}

}

2 个答案:

答案 0 :(得分:-1)

正如“ Gendarme”评论的那样,方法“ nextInt()”将为任何不可接受的(如字符)输入抛出异常。

无论如何,如果您仍希望检查不需要的输入,请在获取用户输入时使用简单的验证。

  <form action="" method="post" novalidate>
    <d1>
      {{ form.hidden_tag() }}
      {{ wtf.form_field(form.word1) }}
      {{ wtf.form_field(form.word2) }}
      <input class="btn btn-primary" type="submit" value="Generate">
    </d1>
  </form>

尽管我个人不喜欢你的逻辑 希望这会有所帮助!

答案 1 :(得分:-1)

我可以建议使用try-catch块来停止输入任何字母的代码。在这种情况下,可以同时使用正整数和负整数。

import java.util.InputMismatchException;
import java.util.Scanner;

public class Store {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

//  Variables
int count = 0;
int sum = 0;
int max = 0;
int min = 0;

System.out.println("Enter numbers or Enter any letter to stop the programm:");
//    A try-catch block to use any char or letter to stop the code and see a result
    try {
//  Initializing minimum value for a reference
        min = scan.nextInt();
        sum += min;
        count++; 
/*  A do-while loop to enter so much values as you'd like, while a letter will not be 
entered **/
        do {
            int number = scan.nextInt();
            sum += number;
            if (number < min) min = number;
            if (number > max) max = number;
            count++;     
        }
        while (scan.hasNext());}
/*  If a letter was entered you catch an exception to normally break the code and see 
a result **/
    catch (InputMismatchException ime) {
    };

    if(count!=0) {
        System.out.println("The Maximum number is: "+ max + 
                "\nThe Minimum number is: "+ min + 
                "\nThe Sum of all entered numbers is: "+sum+
                "\nThe Average of all entered numbers is: "
+ double)sum/(double)count+
                "\nT H E   E N D");
    }
    else {System.out.println("T H E    E N D");}
    scan.close();
    }
}