Java 中的基元类型十进制 - 用户条目



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

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

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

更新

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

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);

基本上,如果有的话,只需删除"+"号,然后解析它。 如果你要做编程,学习正则表达式非常有用,这就是为什么我给你这个,而不是。 但是,如果这是家庭作业,并且您担心如果您使用超出课程范围的内容,老师会产生怀疑,那么请务必不要使用正则表达式方法。

使用扫描仪:

Scanner scan = new Scanner(System.in);
System.out.print("Enter 3 integer numbers: ");
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
System.out.print("You have entered: " + a + " | " + b + " | " + c);

输出:

输入 3 个整数:+3364 -1293 +0007
您输入: 3364 |-1293 |7


旁注:当您将前导 0 与整数(如 0007)一起使用时要小心,因为它在 Java 中被解释为八进制数而不是十进制数。因此,010实际上是在十进制基系统中8的,而不是10

System.out.print(010);

输出:

8

更新:

请注意,Scanner要求符号+-粘在数字上,如 +5432-5432 ,而不是+ 5432- 5432

您可以使用

Scanner类来读取值。 你将不得不做更多的工作来确保 + 不是转换为 Integer 的一部分(检查 Integer 包装类),因为Scanner不会接受 + 作为一元运算符(它适用于负数)。

看看定点数。类可以为您设置此格式。

相关内容

最新更新