当我运行我的代码时,它执行0 0 0 0 0 0 0 0 0?

时间:2017-03-01 05:20:31

标签: java

  import java.util.*;

  /*  For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. 
   * Write a program to find all Armstrong number in the range of 0 and 999.
   */


  public class Armstrong {

    public static void main(String[] args) {
        int no=1,rem=1,mod=1,armstrong;
        //check for armstrong
        System.out.println("Enter armstrong no to be checked: ");
        Scanner sc=new Scanner(System.in);
        no=sc.nextInt();
        armstrong=no;
                while(no>=0)
                {
                    System.out.println(no);
                mod=no%10;
                System.out.println(no);
                rem=rem+(mod*mod*mod);
                System.out.println(no);
                no=no/10;
                System.out.println(no);
                }
                if(no==armstrong)
                    System.out.println("it is armstrong no");

      }

   }

1 个答案:

答案 0 :(得分:0)

  1. 避免无限循环。改变

    while(no>=0)
    

    while(no>0)
    

    由于no=no/10;可以将正数减少到0。不低于此。

    1. 分配rem=0,因为这是您计算总和的地方。