我正在做一个练习,用户必须使用Java编程语言输入带符号的四位数十进制数字,例如+3364-1293+0007等。

据我所知,Java不支持原始类型的十进制。
我的问题是:


如何输入上述数字?
如何为上述数字提供+,-号?


更新

下面的代码显示了一个片段,其中要求用户输入有效的数字(无字符)-一元+在使用下面的代码时不起作用!有没有办法解决它。

public int readInt() {
    boolean continueLoop = true;
    int number = 0;
    do {
        try {
            number = input.nextInt();
            continueLoop = false;
        } // end try
        catch (InputMismatchException inputMismatchException) {
            input.nextLine();
            /** discard input so user can try again */
            System.out.printf("Invalid Entry ?: ");
        } // end of catch
    } while (continueLoop); // end of do...while loop

    return number;
} // end of method readInt()

最佳答案

Java有8种原始(非对象/非参考)类型:


boolean
char
byte
short
int
long
float
double


如果用“十进制”表示“以10为基数的整数”,那么是的,Java通过byteshortintlong支持。您使用哪一个取决于输入范围,但是从我所见,int是最常见的。

如果用“十进制”表示类似于C#的Decimal类型的“以绝对精度为基数的10个有符号浮点数”,那么否,Java没有。

如果Scanner.nextInt为您抛出错误,例如我,那么以下方法应该起作用:

/* Create a scanner for the system in. */
Scanner scan = new Scanner(System.in);
/*
 * Create a regex that looks for numbers formatted like:
 *
 * A an optional '+' sign followed by 1 or more digits; OR A '-'
 * followed by 1 or mored digits.
 *
 * If you want to make the '+' sign mandatory, remove the question mark.
 */
Pattern p = Pattern.compile("(\\+?(\\d+))|(\\-\\d+)");

/* Get the next token from the input. */
String input = scan.next();
/* Match the input against the regular expression. */
Matcher matcher = p.matcher(input);
/* Does it match the regular expression? */
if (matcher.matches()) {
    /* Declare an integer. */
int i = -1;
/*
 * A regular expression group is defined as follows:
 *
 * 0 : references the entire regular expression. n, n != 0 :
 * references the specified group, identified by the nth left
 * parenthesis to its matching right parenthesis. In this case there
 * are 3 left parenthesis, so there are 3 more groups besides the 0
 * group:
 *
 * 1: "(\\+?(\\d+))"; 2: "(\\d+)"; 3: "(\\-\\d+)"
 *
 * What this next code does is check to see if the positive integer
 * matching capturing group didn't match. If it didn't, then we know
 * that the input matched number 3, which refers to the negative
 * sign, so we parse that group, accordingly.
 */
if (matcher.group(2) == null) {
    i = Integer.parseInt(matcher.group(3));
} else {
    /*
     * Otherwise, the positive group matched, and so we parse the
     * second group, which refers to the postive integer, less its
     * '+' sign.
     */
        i = Integer.parseInt(matcher.group(2));
    }
    System.out.println(i);
} else {
    /* Error handling code here. */
}


另外,您也可以这样:

    Scanner scan = new Scanner(System.in);
    String input = scan.next();
    if (input.charAt(0) == '+') {
        input = input.substring(1);
    }
    int i = Integer.parseInt(input);
    System.out.println(i);


基本上,只要有一个就删除“ +”号,然后对其进行解析。如果您打算进行编程,那么学习正则表达式将非常有用,这就是我为您提供正则表达式的原因。但是,如果这是家庭作业,并且您担心如果您使用的内容超出课程范围,则老师会感到怀疑,那么切勿使用正则表达式方法。

关于java - Java中的原始类型十进制-用户输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10064791/

10-12 00:30
查看更多