- package com.itheima.io;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- //接收键盘输入字符,判断是否是回文数(如果不是数字,要做出提示),输入任意大小写over,程序退出.
- public class Test {
- public static void main(String[] args)throws IOException
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String line = null ;
- int flag = 0;//0为不是回文数
- StringBuilder sb = new StringBuilder();
- System.out.println("请输入数字(输入over可退出本程序):");
- while((line = br.readLine())!=null)
- {
- if(line.toUpperCase().equals("OVER"))
- {
- System.out.println("退出程序!");
- break;
- }
- for(Character c :line.toCharArray())
- {
- flag = c<'0'||c>'9'?2:0;//2为不是数字
- sb.append(c);
- }
- flag = flag!=2&&line.equals(sb.reverse().toString())?1:flag;//在是数字的情况下判断是否回文数(1)
- sb.delete(0, sb.length());
- line = flag==1?line+"是回文数,请重新输入:":line+"不是回文数,请重新输入:";//以下两句line另作返回语句用
- line = flag==2?"不是数字,请重新输入":line;
- System.out.println(line);
- }
- br.close();
- }
- }
复制代码 |