日期:2014-05-20 浏览次数:20704 次
package com.kang.test01; public class Exercise01 { private int num = 0; private int levelOne =0; private int levelTwo =0; private int levelThree =0; private int levelFour =0; // private int temp1 = 0; public Exercise01(int num) { // TODO Auto-generated constructor stub this.num = num; if(!this.checkRight(num)) { System.out.println("Your number is not right for here !" + "检查是否是四位数 系统退出"); System.exit(-1); } } /*1、 使用算术运算符得到一个4位十进制数的各位数 * 字并输出,然后输出该数的逆序数和各位数字平方后相加的和。*/ //查一下是否是四位数 public boolean checkRight(int num) { if(num >=1000 && num <=9999) { return true; } return false; } //输出该数的逆序数 public void reverse() { System.out.println(""+this.levelOne+this.levelTwo+this.levelThree+this.levelFour); } public void getEachNum() { System.out.println ("这个数的千位是"+this.getLevelFour() + "\t" +"这个数的百位是"+ this.getLevelThree() + "\t" +"这个数的十位是"+this.getLevelTwo() + "\t" +"这个数的个位是"+ this.getLevelOne() ); } //取这个数的个位 public int getLevelOne() { this.levelOne = num % 1000 % 100 % 10; return levelOne; } //取这个数的十位数 public int getLevelTwo() { this.levelTwo = num %100 / 10; return levelTwo; } //取这个数的百位数 public int getLevelThree() { this.levelThree = num % 1000 / 100; return levelThree; } //取这个数的千位数 public int getLevelFour() { this.levelFour = num / 1000; return levelFour; } public int getSumWithEach() { return levelFour + levelThree + levelTwo + levelOne; } }