我想用AWT或摇摆打印1之间的所有阿姆斯特朗号1000中的文本框,但我只能通过我的code。所以PLC获取最后一个值帮帮我 [英] i want to print all armstrong number between 1 to 1000 in a textbox using awt or swing but i only get last value by my code .So plc help me

查看:566
本文介绍了我想用AWT或摇摆打印1之间的所有阿姆斯特朗号1000中的文本框,但我只能通过我的code。所以PLC获取最后一个值帮帮我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用AWT或摇摆打印1之间的所有阿姆斯特朗号1000中的文本框,但我只能通过我的$ C $得到最后的值c。所以请帮助我

 公共无效的actionPerformed(ActionEvent的五)
    {
        字符串S1 = tf.getText();
        INT N1 =的Integer.parseInt(S1);
        对于(INT N = 0; N< 10000; N ++)
        {
            INT总和= 0;
            INT数= N;
            诠释原有=号码;
            而(数大于0)
            {
            INT R =数10%;
            总和+ = R * R * R;
            数=号/ 10;
            }
            如果(总和==原创)
            {
                tf1.setText(将String.valueOf([i]原));            }
        }
    }


解决方案

对于那些谁也不知道,阿姆斯壮号码(或水仙花数)为N位的数字等于其每个数字的总和的n次幂。

(x1*10(n-1))+(x1*10(n-2))...+(x1*10(n-n)) =(X 1 N +(X 2 N ... +(X ñ N

这意味着,如果编号为1位数,电源会 1

因此​​有是阿姆斯特朗的数字10 1位号码:


  1. 0 = 0 1

  2. 1 = 1 1

  3. = 2 1

  4. 3 = 1

  5. 4 = 4 1

  6. 5 = 5 1

  7. 6 = 6 1

  8. 7 = 7 1

  9. 8 = 8 1

  10. 9 = 9 1

您code,书面,不会确定任何这些数字作为阿姆斯特朗的号码。

您code也将错误地识别一些数字为4位数字阿姆斯特朗因为你只认准立方体(3次方)你的号码4次方的不是。结果
<子>(您不必担心三三两两因为没有两位阿姆斯特朗数字)


为了正确确定1和10000之间的所有可能的阿姆斯特朗数字,你需要写一个权力的循环,通过数乘以 N 倍。

这看起来是这样的:

  // ...你原来的功能开始
//添加一个字符串来保存打印之前所有值
琴弦固定=;
对于(INT N = 0; N&LT; 10000; N ++){
  INT总和= 0;
    // N =原来你有重复的变量(只使用n作为原创)
  INT数= N;
    //虽然仍有数位左
  而(数大于0){
      //获取最小的数字
    INT R =数10%;
      // ----------电源循环-----------
    INT富= N;
      //一旦小于10,它仅仅是为1的功率(它本身)
    而(富&GT; = 10){
        //这意味着富=富/ 10
      富/ = 10;
        //这意味着R = R * R
      R * = R;
    }
      //这意味着总和=总和+ R
    总和+ = R;
      //你应该现在有它的窍门
    数/ = 10;
  }
    //如果总和等于原始数
  如果(总和== N){
      //把这个数字变成一个字符串的结尾(通过换行分隔`\\ N`)
    支架+ = N +\\ n;
  }
}
  //全部完成,所以设置文本框的值
tf1.setText(保持器);
// ...无论code要完成了

这与文本框也应该照顾你的问题得到每次覆盖。通过保存的数字转换成字符串,然后打印所有的人都在一次,只有一次(无覆盖),你会得到更好的结果。

i want to print all armstrong number between 1 to 1000 in a textfield using awt or swing but i only get last value by my code .So pls help me

    public void actionPerformed(ActionEvent e)
    {
        String s1=tf.getText();
        int n1=Integer.parseInt(s1);
        for(int n=0;n<10000;n++)
        {
            int sum=0;
            int number=n;
            int original=number;
            while(number>0)
            {
            int r=number%10;
            sum+=r*r*r;
            number=number/10;
            }
            if(sum==original)
            {
                tf1.setText(String.valueOf(original[i]));

            }
        }
    }

解决方案

For those who don't know, an Armstrong number (or narcissistic number) is a number with n digits that is equal to the sum of each of its digits to the nth power.

(x1*10(n-1))+(x1*10(n-2))...+(x1*10(n-n)) = (x1)n+(x2)n...+(xn)n

This means that if the number is 1 digit, the power will be 1.

Therefore there are 10 1 digit numbers that are Armstrong numbers:

  1. 0 = 01
  2. 1 = 11
  3. 2 = 21
  4. 3 = 31
  5. 4 = 41
  6. 5 = 51
  7. 6 = 61
  8. 7 = 71
  9. 8 = 81
  10. 9 = 91

Your code, as written, will not identify any of those numbers as Armstrong numbers.

Your code will also incorrectly identify some numbers as 4 digit Armstrong numbers because you only look for the the cubes (3rd power) of your numbers not the 4th power.
(You don't have to worry about twos because there are no two digit Armstrong numbers)


In order to correctly determine all the possible Armstrong numbers between 1 and 10000, you need to write a "power" loop that finds the nth power of a number by multiplying the number n times.

This would look something like:

//... beginning of your original function
//added a string to hold all the values before printing
string holder = "";
for(int n=0;n<10000;n++){
  int sum=0;
    //n=original you had duplicate variables (just use n as original)
  int number = n;
    //while there are still digits left
  while(number>0){
      //get the smallest digit
    int r=number%10;
      //----------"Power" loop-----------
    int foo = n;
      //once smaller than 10, it's only a power of 1 (which is itself)
    while(foo>=10){
        //this means foo = foo/10
      foo /= 10;
        //this means r = r*r
      r*=r;
    }
      //this means sum = sum+r
    sum += r;
      //you should have the hang of it by now
    number/=10;
  }
    //if the sum equals the original number
  if(sum==n){
      //put that number into the end of a string (separated by newlines `\n`)
    holder+=n+"\n";
  }
}
  //All done, so set the text box value
tf1.setText(holder);
//... whatever code you want to finish up

This should also take care of your problem with the textBox getting overwritten each time. By saving the numbers into a string and then printing all of them at once, only once (no overwriting), you'll get better results.

这篇关于我想用AWT或摇摆打印1之间的所有阿姆斯特朗号1000中的文本框,但我只能通过我的code。所以PLC获取最后一个值帮帮我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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