集册 Java实例教程 从控制台读取数字并检查它是否是回文数字

从控制台读取数字并检查它是否是回文数字

欢马劈雪     最近更新时间:2020-01-02 10:19:05

516
从控制台读取数字并检查它是否是回文数字
/* 
*来 自
 n o w j a v a . c o m
*/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;


public class Main {


  public static void main(String[] args) {


    System.out.println("Enter the number to check..");

    int number = 0;


    try {

      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      number = Integer.parseInt(br.readLine());
      /* from 
      N o  w  J a v a . c o m - 时  代  Java*/

    } catch (NumberFormatException ne) {

      System.out.println("Invalid input: " + ne);

      System.exit(0);

    } catch (IOException ioe) {

      System.out.println("I/O Error: " + ioe);

      System.exit(0);

    }


    System.out.println("Number is " + number);

    int n = number;

    int reversedNumber = 0;

    int temp = 0;


    while (n > 0) {

      temp = n % 10;

      n = n / 10;

      reversedNumber = reversedNumber * 10 + temp;

    }


 
展开阅读全文