两个JAVA的小程序希望有大神来解答
假设n!小于等于int类型的最大值,(n+1)!大于int类型的最大值,请编写程序计算n的值并打印出来。类 Integer 的 MAX_VALUE 属性,是一个常量,保存了int的最大值
package cn.demo; import cn.exception.*; import cn.exception.customException.*; public class Test { public Test() { int i; for (i=1;i<Integer.MAX_VALUE/10;i*=10) testMain(i); testMain(Integer.MAX_VALUE); } public void testMain(int n) { int result; try { System.out.println("测试数据:"+n); result=test(n); System.out.println("运行结果为:"+result+"\n"); } catch(CustomException e) { System.out.println(e.message+"\n"); } } private static int test(int testNum) throws IntegerException { int n=1; int count=1; if (testNum<0) throw new IntegerException(); while (testNum/n>=count+1) n*=++count; return count; } public static void main(String[] arge) { new Test(); } }
package cn.exception.customException; //自定义异常类 public abstract class CustomException extends Exception { public String message; public String toString() { return message; } }
package cn.exception; import cn.exception.customException.*; public final class IntegerException extends CustomException { public final IntegerException() { super.message="测试数据不能小于0"; } }
[此贴子已经被作者于2018-3-17 16:18编辑过]
package com.xiaoa.demo; public class Integer { public static final int MAX_VALUE = 0; }
package com.xiaoa.demo; public class Test { public Test() { int i; for (i = 1; i < Integer.MAX_VALUE / 10; i *= 10) testMain(i); testMain(Integer.MAX_VALUE); } public void testMain(int n) { int result; try { System.out.println("测试数据:" + n); result = test(n); System.out.println("运行结果为:" + result + "\n"); } catch (CustomException e) { System.out.println(e.message + "\n"); } } private static int test(int testNum) throws IntegerException { int n = 1; int count = 1; if (testNum < 0) throw new IntegerException(); while (testNum / n >= count + 1) n *= ++count; return count; } public static void main(String[] arge) { new Test(); } } //自定义异常类 abstract class CustomException extends Exception { public String message; public String toString() { return message; } } final class IntegerException extends CustomException { public IntegerException() { super.message="测试数据不能小于0"; } }
[此贴子已经被作者于2018-3-17 19:14编辑过]