标签:bsp color try class blog print 方法 ret 输入
例如,首先输入 2 4就是计算2的4次方,如果出现负数,则抛出异常
代码如下:
import java.util.*; import java.util.Scanner; class MyCalculator { long power(int n, int p) throws Exception { if (n < 0 || p < 0) { throw new Exception("n and p should be non-negative"); } else { return (long) Math.pow(n, p); } } } class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); while ( in .hasNextInt()) { int n = in .nextInt(); int p = in .nextInt(); MyCalculator my_calculator = new MyCalculator(); try { System.out.println(my_calculator.power(n, p)); } catch (Exception e) { System.out.println(e); } } } }
标签:bsp color try class blog print 方法 ret 输入
原文地址:http://www.cnblogs.com/Angella/p/6602175.html