简体   繁体   English

Java按数学顺序打印出所有数字

[英]Java Print out all numbers in a math-sequence

I want the program to print out every number that each digit to the power to 3 added together will equal that number. 我希望程序打印出每个数字,即将幂加到3的每个数字加起来等于该数字。 For instance, the number 153: 1^3 + 5^3 + 3^3 = 153. 例如,数字153: 1^3 + 5^3 + 3^3 = 153.

This is the code, but I do not know why it does not work. 这是代码,但是我不知道为什么它不起作用。

Edit: The reason I have siffra++; 编辑:我有siffra++;的原因siffra++; is because I want it to test all numbers to 500 . 是因为我要它测试所有数到500 This is what I got now, but it does still now work (not showing anything in JOptionPane ): 这就是我现在得到的,但是现在仍然可以工作(在JOptionPane不显示任何内容):

int siffra = 153;

String allaSiffror = "";
int count = 0;

if (siffra < 500) { 
    for (int i = 0; i < 3; i++) {
        String siffran = Integer.toString(siffra);
        String selekt = siffran.substring(i,i+1);
        int selekta = Integer.parseInt(selekt);
        count += Math.pow(selekta,3);
            if (count == siffra) {
                System.out.println(count);
                allaSiffror += count + " ";
                count = 0;
            } 
            siffra++;
    }
}


JOptionPane.showMessageDialog(null, allaSiffror);

the problem is that this part is outside of the loop: 问题是这部分不在循环中:

String siffran = Integer.toString(siffra);
int count = 0;

so your count adds up every number to the previous, and since you increase siffra but work on siffran- you're checking 100 all the time 因此您的计数将每个数字与上一个数字相加,并且由于增加了siffra而使用了siffran,因此您一直都在检查100

aside from that, I'd also take @Sluth's advice - no need to convert back and forth to string and int - use mathematical mod and % to get the digits 除此之外,我还接受@Sluth的建议-无需来回转换为字符串和整数-使用数学mod和%获得数字

Such numbers are called Armstrong numbers 这样的数字称为阿姆斯特朗数字
Firstly your mistakes 首先,你的错误
1) Your are making count(sum I assumed) 0 only if you find number matching your condition like 153...what if number is not matching then shouldn't the count/sum be zero for new number. 1)仅当您发现与条件相匹配数字(如153)时,您的计数(我假设的总和)为0 ... 如果数字不匹配,那么新数字的计数/总和不应为零
2) Secondly you are saying that, you want to test numbers till 500 but you are using if condition which would be exectued only once ...so use loop over there 2) 其次,您说的是,您要测试直到500的数字,但是您使用的是if条件,该条件只能执行一次 ...所以请在那儿使用循环

Few suggestions I would make 我会提出一些建议
1)Instead of using String use StringBuilder. 1)代替使用String,而使用StringBuilder。
2)Also instead of converting integer to string and then using substring directly work with integer only 2)也不要将整数转换为字符串然后使用子字符串直接与整数一起使用

So modify code as follows 所以修改代码如下

 int siffra = 153;

 StringBuilder allaSiffror = new StringBuilder();
 int count;

 while(siffra<500){
     count = 0;
     int temp = siffra;

     int num;
     while(temp>0){
       num=temp%10;  
       temp=temp/10;
       count += Math.pow(num,3);  
     }

     if (count == siffra){
       System.out.println(count);
       allaSiffror.append(count + " ");
     } 
     siffra++;
 }
System.out.println(allaSiffror.toString());


JOptionPane.showMessageDialog(null, allaSiffror.toString());

Update o/p is as follows : 更新o / p如下:

 153 370 371 407 153 370 371 407 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM