JAvaプログラミング問題の水仙数
4363 ワード
対象に向かって水仙の処理――対象パッケージユーザ操作クラスUserShuiXianHua.java 水仙の具体的な操作類ShuiXianHua.java
package com.ucai.question.shuixianhua;
/**
* ,
* @author F
*
*/
public class UserShuiXianHua {
public static void main(String[] args) {
//
ShuiXianHua shui = new ShuiXianHua();
//
shui.panduan();
}
}
package com.ucai.question.shuixianhua;
import java.util.Scanner;
import com.ucai.check.RegExpValidatorUtils;
/**
*
*
* : ,
* : ,
*
* @author F
*
*/
public final class ShuiXianHua {
/**
* , ,
* ,
* :1 ,2
* 1,
* 2, ,
*/
@SuppressWarnings("resource")
public void panduan() {
System.out.println(" :
1: ,
2: ");
System.out.println(" :");
Scanner sc = new Scanner(System.in);
String str = "";
outer: while (true) {
str = sc.next();
if (!RegExpValidatorUtils.IsIntNumber(str) || str.length() > 1) {
System.out.println(" " + str + ", !");
continue;
}
switch (str) {
case "1":
if (!shuixianhua()) {
break outer;
}
break;
case "2":
if (!shuixianhuas()) {
break outer;
}
break;
}
}
}
/**
*
*
* @return
*/
@SuppressWarnings("resource")
private boolean shuixianhua() {
Scanner sc = new Scanner(System.in);
String str = "";
System.out.println(" :
:exit
:return
:");
while (true) {
str = sc.next();
if ("exit".equals(str.trim())) {
System.out.println(" !");
return false;
}
if ("return".equals(str.trim())) {
System.out.println(" !");
panduan();
}
if (!RegExpValidatorUtils.IsIntNumber(str) && !"exit".equals(str.trim())) {
System.out.println(" !");
continue;
}
checkShuixianhua(str);
}
}
/**
*
* @param str
*/
private void checkShuixianhua(String str){
StringBuilder sbsum = new StringBuilder();
long sum = 0;
for (int i = 0; i < str.length(); i++) {
double k = Double.parseDouble(String.valueOf(str.charAt(i)));
sbsum.append(Math.round(k) + "^3 + ");
sum += Math.pow(k, 3);
}
sbsum.delete(sbsum.lastIndexOf("+"), sbsum.length());
StringBuilder sb = new StringBuilder(str);
if (Double.parseDouble(str) == sum) {
sb.append(" = ").append(sbsum).append(", , ").append(str);
sb.append(" ");
} else {
sb.append(" != ").append(sbsum).append(", , ").append(str);
sb.append(" ");
}
sb.append(" ");
System.out.println(sb);
}
/**
* ,
*
* @return
*/
@SuppressWarnings("resource")
private boolean shuixianhuas() {
Scanner sc = new Scanner(System.in);
String str = "";
System.out.println(" ( :1~1000):
:exit
:return
:");
while(true){
str = sc.next();
if ("exit".equals(str.trim())) {
System.out.println(" !");
return false;
}
if ("return".equals(str.trim())) {
System.out.println(" !");
panduan();
}
if(!checkFormat(str)){
System.out.println(" ");
continue;
}
String[] strs = str.split("~");
shuixianhua(Long.parseLong(strs[0]),Long.parseLong(strs[1]));
continue;
}
}
/**
*
* @param start
* @param end
*/
private void shuixianhua(long start,long end){
System.out.println(start+"~"+end+" :");
for(;start