集册 Java实例教程 获取使用while循环避免异常的Integer方法。

获取使用while循环避免异常的Integer方法。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

397
获取使用while循环避免异常的Integer方法。

import java.util.*;


public class GetInteger2

{/*n o w    j a v a  . c o m 提供*/

    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args)

    {

        System.out.print("Enter an integer: ");

        int i = GetInteger();

        System.out.println("You entered " + i);

    }


    public static int GetInteger()

    {//来 自 时 代      J a v a   公   众 号 - nowjava.com

        while (!sc.hasNextInt())

        {

            sc.nextLine();

            System.out.print(
展开阅读全文