简体   繁体   English

如何判断整数只有2位数字?

[英]How can I tell that an integer is only 2 digits long?

I need to write a Java program that prompts the user to enter an integer consisting of exactly 2 digits; 我需要编写一个Java程序来提示用户输入一个正好由2位数字组成的整数。 then displays on the screen the sum of its individual digits. 然后在屏幕上显示其各个数字的总和。

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! 最后一行包含错误!

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
}

The variable x is of type int , so you can't call a method on it. 变量x的类型为int ,因此您不能在其上调用方法。 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. 您需要将输入读取为String或将int转换为String然后调用length() ,或者只是测试int1099之间(包括1099 (包括1099 )。

In a programming langauge, there are things called L-values and R-values. 在编程语言中,有些东西称为L值和R值。 In an assignment operation, a L-value can accept a R-value as input. 在赋值操作中,L值可以接受R值作为输入。 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值。

x = 5;

x is the L-value and 5 is the R-value. x是L值,5是R值。 It is possible to assign five to x. 可以为x分配5。

However, a function returns a R-value. 但是,函数返回R值。 Therefore, it is possible to do this 因此,可以这样做

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 names which represent a value , but R-values are values or 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 现在,如果您使用了equals比较运算符,则两个值都必须是R值,因为没有执行赋值操作

a.length == x

is just fine, because it is not the assignment operator = but rather one of the comparison operators == . 很好,因为它不是赋值运算符= ,而是比较运算符==

Your error comes because x is a primitive, not an object. 出现错误是因为x是原始图元,而不是对象。 Only objects have methods like length() . 只有对象具有诸如length()类的方法。 A quick an easy way to determine the length of an integer is by using Math.log() . 一种确定整数长度的简便方法是使用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;

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的方法来找到int的长度,但可以找到String的长度。

Try converting the int to a String and finding the length of that: 尝试将int转换为String并找到其长度:

boolean isTwoDigits = x.toString().length() == 2;

You cannot call length on integer just write 你不能在整数上调用长度只是写

if(x>=10 && x<=99)
{
//write your code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何生成整数数字的平方 - How can I generate the squares of the digits of an integer 如何判断Java整数是否为空? - How can I tell if a Java integer is null? 如何使用Integer.toBinaryString设置Java中的位数? - How with Integer.toBinaryString can I set the amount of digits in java? 在Java中,如何获得数字字符串的子序列,其索引长度可以为10 ^ 5位数 - In Java how can I get a subsequence of number string whose indices can be as 10^5 digits long 如何在不使用控制语句或循环的情况下将整数中的所有数字(在000和1000之间)相乘? - How can I multiply all the digits in an integer (between 000 & 1000 exclusive) without using control statements or loops? 如何获得由整数的某些数字组成的最大数字 - How can I get max number that consists of some digits of an integer number 如何仅允许 integer 并且不允许超过 9 位数字 - how to allow integer only AND not allowing more than 9 digits 如何只保留 integer 的最后 3 位数字并能够操作它们? - How to keep only last 3 digits of an integer and be able to manipulate them? 如何反转整数的数字? - How to reverse digits of integer? 如何告诉querydsl-maven-plugin使用Long而不是BigDecimal生成NumberPath? - How can I tell querydsl-maven-plugin to generate NumberPath with Long instead of BigDecimal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM