一、代码如下
package com.wujianbo;
import java.util.Scanner;
/*
* x的y次方
*/
public class Demo08 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo08 demo08= new Demo08();
//demo08.powerOne();
//System.out.println(demo08.powerTwo(2, 3));
demo08.exePower();
}
public void powerOne() {
// 2的3次方
int a = 3;
int b = 5;
int result = 1;
// 循环b次,基数为a
for (int i = 1; i <= b; i++) {
result *= a;
}
System.out.println(a + "的" + b + "次方是:" + result);
}
// 求a的b次方为:
public double powerTwo(double a, double b) {
// int a = a;
// int b = b;
int result = 1;
// 循环b次,基数为a
for (int i = 1; i <= b; i++) {
result *= a;
}
//System.out.println(a + "的" + b + "次方是:" + result);
return result;
}
public void exePower() {
//输入基数、次方的数
while(true) {
Scanner sc= new Scanner(System.in);
System.out.println("请输该冥次方函数的基数:");
int a= sc.nextInt();
System.out.println("请输入该冥次方行数的冥方数:");
int b= sc.nextInt();
//调用冥方函数进行运算
double result= this.powerTwo(a, b);
//输出返回的值
System.out.println(a + "的" + b + "次方是:" + result);
if(a == 2) {
break;
}
}
}
}