[program] ReverseDigits ( 即: 倒序输出int类型的数字,比如1792,输出为2971)


  Write a program named "ReverseDigits ", it generates the number that has the same digits in the reverse order, as illustrated by this sample run:

[program] ReverseDigits ( 即: 倒序输出int类型的数字,比如1792,输出为2971)

---------------------------------ReverseDigits 类---------------------------------

package chapter4;

import java.util.InputMismatchException;
import java.util.Scanner;

/*
 * 倒序输出int类型的数字,比如1792,输出为2971
 *
 * */


public class ReverseDigits {

    int N=0;
    int subnum=0;
   
 public void run(){
      System.out.println("please input number  : ");
      Scanner input=new Scanner(System.in);
  
      try {
         int num=input.nextInt();
   
         while (num>0){
    
         subnum=num%10;
         num=num/10;
    
        N =(N+subnum)*10;   //算法的关键~
      }
   
         N =N/10;    //算法的关键
  
      System.out.println("the ReverseDigits is :"+ N);
   
      } catch(InputMismatchException e1){
     
         System.out.println("输入数据类型不符,应为int 类型");

      }
  
   }
 }

--------------------------主函数调用--------------------------------

package chapter4;

public class main4 {

 /**
  * 主要运行<<the art and science of java>>第4章的练习题目
  */
 public static void main(String[] args) {

  ReverseDigits rev1=new ReverseDigits();
  rev1.run();

 }

}

你可能感兴趣的:(git)