Java 我怎么知道一个整数只有 2 位数长?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19730665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I tell that an integer is only 2 digits long?
提问by whoseee
I need to write a Java program that prompts the user to enter an integer consisting of exactly 2 digits; then displays on the screen the sum of its individual digits.
我需要编写一个 Java 程序,提示用户输入一个由 2 位数字组成的整数;然后在屏幕上显示其各个数字的总和。
I am stuck here. What am I doing wrong?
我被困在这里。我究竟做错了什么?
import java.util.Scanner ;
public class ss {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int x;
System.out.println("Please Enter a number consists of 2 digits only : ");
x = input.nextInt();
x.length() == 2;
}
}
and the last line contains an error!
最后一行包含错误!
回答by óscar López
Assuming that x
is positive, a simple way to check if it has exactly two digits would be:
假设这x
是肯定的,检查它是否正好有两位数的简单方法是:
if (x >= 10 && x <= 99) {
// x contains exactly two digits
}
回答by rgettman
The variable x
is of type int
, so you can't call a method on it. You need to either read the input as a String
or convert the int
to a String
then call length()
, or just test that the int
is between 10
and 99
, inclusive.
该变量x
的类型为int
,因此您不能对其调用方法。您需要将输入读取为 aString
或将 the 转换int
为String
then call length()
,或者只是测试int
介于10
和之间99
,包括。
回答by Edwin Buck
In a programming langauge, there are things called L-values and R-values. In an assignment operation, a L-value can accept a R-value as input. This comes from the typical layout which has L-values on the left of the assignment operator and R-values on the right side of the assignment operator.
在编程语言中,有称为 L 值和 R 值的东西。在赋值操作中,L 值可以接受 R 值作为输入。这来自典型的布局,其中 L 值位于赋值运算符的左侧,而 R 值位于赋值运算符的右侧。
x = 5;
x is the L-value and 5 is the R-value. It is possible to assign five to x.
x 是 L 值,5 是 R 值。可以将 5 分配给 x。
However, a function returns a R-value. Therefore, it is possible to do this
但是,函数返回 R 值。因此,可以做到这一点
x = a.length();
but is is not possible to do
但这是不可能的
a.length() = x;
because you can not assign a value to the return of a function.
因为你不能为函数的返回值赋值。
Fundamentally, L-values are nameswhich represent a value, but R-values are valuesor items which when analyzed result in the return of values.
从根本上说,L值是名称,其代表一个值,但R值是值,其在返回解析结果时或项目值。
Now, if you used the equals comparison operator, both values must be R-values, because no assignment is being performed
现在,如果您使用等于比较运算符,则两个值都必须是 R 值,因为没有执行赋值
a.length == x
is just fine, because it is not the assignment operator =
but rather one of the comparison operators ==
.
很好,因为它不是赋值运算符=
,而是比较运算符之一==
。
回答by Justin
Your error comes because x
is a primitive, not an object. Only objects have methods like length()
. A quick an easy way to determine the length of an integer is by using Math.log()
.
你的错误是因为它x
是一个原始的,而不是一个对象。只有对象具有像length()
. 确定整数长度的一种快速简便的方法是使用Math.log()
.
public int length(int n){
if (n == 0) return 1; // because Math.log(0) is undefined
if (n < 0) n = -n; // because Math.log() doesn't work for negative numbers
return (int)(Math.log10(n)) + 1; //+1 because Math.log10 returns one less
//than wanted. Math.log10(10) == 1.
}
This method uses the fact that the base b logarithm of an integer a is related to the length of the integer a.
该方法利用了一个事实,即整数 a 的底 b 对数与整数 a 的长度有关。
Or, if you don't know how to use methods, you could do this (assuming n is the integer to check):
或者,如果您不知道如何使用方法,您可以这样做(假设 n 是要检查的整数):
int length = (n == 0)? 1: ((n > 0)? (int) (Math.log(n)) + 1: (int) (Math.log(-n)) + 1);
Or, if you don't use the ternary operator, you could expand it:
或者,如果您不使用三元运算符,则可以扩展它:
int length = -1; //placeholder; might not need it.
if (n == 0) length = 1;
else if (n > 0) length = (int) (Math.log(n)) + 1;
else length = (int) (Math.log(-n)) + 1;
回答by earora4498
You can't find the length of an int
by calling a method on it, but you can find the length of a String
.
您无法int
通过对其调用方法来找到an 的长度,但是您可以找到 a 的长度String
。
Try converting the int
to a String
and finding the length of that:
尝试将int
a转换为 aString
并找到它的长度:
boolean isTwoDigits = x.toString().length() == 2;
回答by Gourav
You cannot call length on integer just write
你不能在整数上调用 length 只写
if(x>=10 && x<=99)
{
//write your code here
}