简单的数字与个人数字数组 [英] Simple Number to Array with Individual Digits

查看:100
本文介绍了简单的数字与个人数字数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是非常新的节目,但我在努力改善我的技能作为一个程序员。目前,我正在一个问题,我给了自己在那里我想带可变数量,使每一个数字它在阵列中一个单独的号码。我不关心数字的顺序,因此如果他们被逆转,那么它并不重要,我。我认识的人问这个问题无数次,但他们似乎总是用很多的东西,我不明白。由于我校不提供任何Java课程,我只知道我有我自己的教训,所以如果你能解释一下你在code是不是非常琐碎的使用的任何条款,那将是美好的。现在,我已经写了:

  INT数= 1234567890;
    而(数大于0){
        的System.out.println(数%10);
        数=号/ 10;

这工作正常打印单独的数字,但我无法弄清楚如何将它们添加到阵列。我非常AP preciate任何帮助,您可以给,并请记住,我更preFER简单过小的尺寸。谢谢你在前进!

P.S。我已经看到了类似问题的一些反应包括什么,我认为是字符串数组。为了让我有工作仍然正常工作程序的一部分,我想我需要使用整型数组。如果好奇,则code的其余部分被用来确定是否阵列中的数字是各不相同,以达到确定一个数的数字是所有不同的最终结果。它看起来是这样的:

  INT重复= 0;
INT [] digitArray;
digitArray =新INT [10];
的for(int i = 0; I< digitArray.length;我++)
    对于(INT J = 0; J< digitArray.length; J ++)
        如果((ⅰ= j的)及!及(digitArray [Ⅰ] == digitArray [j]的))唯一=独特+ 1;
的System.out.println(唯一== 0);


解决方案

方法number.toString()。长度()将返回的位数。那是一样的需要阵列的长度。然后你用你的code和以前一样,但不是你打印数字添加到阵列。

  INT数= 1234567890;
INT LEN = Integer.toString(数字)。长度();
INT [] = iarray新INT [LEN]
对于(INT指数= 0;指数 - LT; LEN;指数++){
    iarray [索引] =编号%10;
    数/ = 10;
}

I am exceptionally new to programming, but I am working on improving my skills as a programmer. Currently, I am working on a problem I gave myself where I am trying to take a variable number and make each of its digits a separate number in an array. I don't care about the order of the digits, so if they are reversed, then it doesn't matter to me. I know people have asked this question numerous times, but they always seem to use a lot of things that I don't understand. Since my school doesn't offer any Java courses, I only know what I have learned on my own, so if you could explain any terms you use in the code that aren't extremely trivial, that would be wonderful. Right now, I have written:

int number = 1234567890;
    while (number > 0) {
        System.out.println(number%10);
        number = number/10; 

This works fine for printing the digits individually, but I can't figure out how to add them to the array. I greatly appreciate any help you can give, and please keep in mind that I much prefer simplicity over small size. Thank you in advance!

P.S. Some responses I've seen for similar questions include what I think are arrays of strings. In order for the part of the program that I have working to still work, I think that I need to use an array of integers. If you're curious, the rest of the code is used to determine if the numbers in the array are all different, in order to achieve the final result of determining if a number's digits are all distinct. It looks like this:

int repeats=0;
int[] digitArray;
digitArray = new int[10];
for (int i = 0; i < digitArray.length; i++) 
    for (int j = 0; j < digitArray.length; j++)
        if ((i != j) && (digitArray[i]==digitArray[j])) unique = unique+1;
System.out.println(unique==0);

解决方案

Method number.toString().length() will return the number of digits. That is the same as the length of your needed array. Then you use your code as before, yet instead of printing you add the digit to the array.

int number = 1234567890;
int len = Integer.toString(number).length();
int[] iarray = new int[len];
for (int index = 0; index < len; index++) {
    iarray[index] = number % 10;
    number /= 10;
}

这篇关于简单的数字与个人数字数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆