The literal of int xxxxx is out of range

import java.util.Scanner;

/**
 * 任意输入一个整数(小于 10 位),求它的位数
 *
 * @author zzu119
 *
 */
public class digitCount {

    public static void main(String[] args) {
        long absnum = 0;
        long refnum = 999999999;
        while (true) {
            System.out.println("请输入一个小于10位的数字:");
            Scanner input = new Scanner(System.in);
            long num = input.nextLong();
            absnum = Math.abs(num);
            System.out.println("输入数字的绝对值为:" + absnum);
            if (absnum > refnum) {// absnum>9999999999 显示语法错误,The literal of
                                    // type int is out of range
                /*
                 * Add a capital L to the end: long value =
                 * 9223372036854775807L; Otherwise, the compiler will try to
                 * parse the literal as an int, hence the error message
                 */
                System.out.println("请输入小于10位的数字!");
            } else {
                System.out.println("输入为合法数值!");
                break;
            }
        }
        if (absnum > 99999999) {
            System.out.println("输入数字为9位数!");
        } else if (absnum > 9999999) {
            System.out.println("输入数字为8位数!");
        } else if (absnum > 999999) {
            System.out.println("输入数字为7位数!");
        } else if (absnum > 99999) {
            System.out.println("输入数字为6位数!");
        } else if (absnum > 9999) {
            System.out.println("输入数字为5位数!");
        } else if (absnum > 999) {
            System.out.println("输入数字为4位数!");
        } else if (absnum > 99) {
            System.out.println("输入数字为3位数!");
        } else if (absnum > 9) {
            System.out.println("输入数字为2位数!");
        } else {
            System.out.println("输入数字为1位数!");
        }

    }

}

posted on 2015-09-29 13:10  菜鸟Z  阅读(223)  评论(0编辑  收藏  举报

导航