位数以及是哪几位整数

发布于 2024-11-24 02:15:22 字数 76 浏览 0 评论 0原文

示例 int i=185;

然后我想得到 'i' 包含 3 位数字,这些数字是 1,8 和 5。

Example int i=185;

Then I want to get that 'i' contains 3 digits and those digits are 1,8, and 5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

⊕婉儿 2024-12-01 02:15:34

执行此操作的简单方法是转换为与区域设置无关的字符串,然后查看字符串中的每个字符。我不会给出最终的解决方案,以防万一这是家庭作业,但这里有一些重要的 API...

转换为字符串:

String stringForm = Integer.toString(number);

处理负数:

int nonNegative = Math.abs(number);

字符串的长度:

int length = stringForm.length();

获取字符串的第 i 个字符:

char c = stringForm.charAt(i);

The easy way to do this is by converting to a locale-agnostic string, then looking at each character in the string. I am not giving the final solution in case this is homework, but here are some important APIs...

Converting to string:

String stringForm = Integer.toString(number);

Handling negatives:

int nonNegative = Math.abs(number);

Length of a string:

int length = stringForm.length();

Getting the i-th character of a string:

char c = stringForm.charAt(i);
甜是你 2024-12-01 02:15:34

一种方法是:

int i = 185;

int a = i / 100;         // 1
int b = (i % 100) / 10;  // 8
int c = i % 10;          // 5 

但我认为你需要更通用的东西?尝试通过字符串

int i = 185;
String iAsString = String.format("%d", i);

if(iAsString.contains("1")){
   // do something...
}

和更高级:

    int i = 185;
    String iAsString = String.format("%d", i);

    HashSet<Integer> set = new HashSet<Integer>();
    for(char c : iAsString.toCharArray()){
        set.add(Integer.valueOf(String.valueOf(c)));
    }

然后你就可以在集合上工作了。

One way would be:

int i = 185;

int a = i / 100;         // 1
int b = (i % 100) / 10;  // 8
int c = i % 10;          // 5 

But i think you need something more generic? Try via string

int i = 185;
String iAsString = String.format("%d", i);

if(iAsString.contains("1")){
   // do something...
}

And more advanced:

    int i = 185;
    String iAsString = String.format("%d", i);

    HashSet<Integer> set = new HashSet<Integer>();
    for(char c : iAsString.toCharArray()){
        set.add(Integer.valueOf(String.valueOf(c)));
    }

Then you can work on the set.

随波逐流 2024-12-01 02:15:34

对于整数 i,小数位数也由 Math.ceil(Math.log10(i)) 给出。

The number of decimal digits is also given by Math.ceil(Math.log10(i)), for integral i.

七度光 2024-12-01 02:15:33

第一个解决方案:

/**
 * Using Integer/String classes functionality
 */
public class Shweta {

    private static Integer i = 185;

    public static void main(String... args) {
        String iStr = i.toString();
        for (char digit : iStr.toCharArray()) {
            System.out.println(digit);
        }
        System.out.println("Length is: " + iStr.length());
    }

}

第二个解决方案:

/**
 * Doing that in a 'homework' way
 */
public class ShwetaNoCheats {

    private static Integer i = 185;

    public static void main(String... args) {
        int length = 0;
        while (i != 0) {
            System.out.println(i - (i / 10) * 10);
            i /= 10;
            length++;
        }
        System.out.println("Length is: " + length);
    }
}

1st solution:

/**
 * Using Integer/String classes functionality
 */
public class Shweta {

    private static Integer i = 185;

    public static void main(String... args) {
        String iStr = i.toString();
        for (char digit : iStr.toCharArray()) {
            System.out.println(digit);
        }
        System.out.println("Length is: " + iStr.length());
    }

}

2nd solution:

/**
 * Doing that in a 'homework' way
 */
public class ShwetaNoCheats {

    private static Integer i = 185;

    public static void main(String... args) {
        int length = 0;
        while (i != 0) {
            System.out.println(i - (i / 10) * 10);
            i /= 10;
            length++;
        }
        System.out.println("Length is: " + length);
    }
}
帅的被狗咬 2024-12-01 02:15:31

提示:您需要将数字除以 10,以获得最后一位数字。然后将同一个数除以 10,得到前两个数。根据需要重复多次。

Hint: You need to take the modulus of the number by 10, to get the last digit. And then divide the same number by 10, do get the first two numbers. Repeat yourself as many times as required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文