我正在进行一项练习,其中用户必须使用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()
答案 0 :(得分:4)
Java有8种原始(非对象/非对象)类型:
boolean
char
byte
short
int
long
float
double
如果“十进制”表示“基数10有符号整数”,则表示是,Java通过byte
,short
,int
和long
支持。你使用哪一个取决于输入范围,但int
是我见过的最常见的。
如果“十进制”表示“基本10带有绝对精度的带符号浮点数”类似于C#的Decimal
类型,那么不,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);
基本上只删除'+'符号(如果有),然后解析它。如果你要编程,学习正则表达式非常有用,这就是我给你的原因。但是,如果这是家庭作业而你担心如果你使用超出课程范围的东西老师会产生怀疑,那么一定不要使用正则表达方法。
答案 1 :(得分:3)
使用Scanner:
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
附注:当使用带有0007
等整数的前导0时要小心,因为它在Java中被解释为八进制数而不是十进制数。因此,010
实际上是8
在十进制基础系统中,而不是10
。
System.out.print(010);
<强>输出:强>
8
<强>更新强>
请注意,Scanner
要求标记+
和-
与+5432
和-5432
相匹配,而不是+ 5432
也不是- 5432
。
答案 2 :(得分:1)
答案 3 :(得分:0)
看看定点数字。 DecimalFormat类可以为您格式化。