本道题目为北京大学java程序设计的week2作业题。题目如下:
关于验证很简单,所解决题目代码如下:
类1:Mathtest:
package mathtest;
public class MathTest {
private int number;
private int sum;
public int setSum(){
sum=sum+number*number*number;
return sum;
}
public int setNumber(int number){
this.number=number;
return number;
}
public int getNumber(){
return number;
}
public int getSum(){
return sum;
}
}
类2:testresult
package mathtest;
import java.math.BigDecimal;
import java.util.Scanner;
public class TestResult {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("if you want to verify an equation?");
System.out.print("if you want to verify ,please input 1,else you want to find ,please input o:");
int p = scanner.nextInt();
do {
System.out.print("please input the initial postive integer:");
int a = scanner.nextInt();
System.out.print("please input the end postive integer:");
int b = scanner.nextInt();
System.out.print("please input the postive integer to be verified:");
int n = scanner.nextInt();
MathTest test = new MathTest();
for (int i = a; i <= b; i++) {
test.setNumber(i);
test.setSum();
}
if (test.getSum() == n * n * n) {
System.out.println("the equation is true");
} else {
System.out.println("the equation is flase");
}
System.out.print("if you want to verify an equation?");
System.out.print("if you want to continue ,please input 1,else input 0:");
p = scanner.nextInt();
}while(p==1);
System.out.println("find more equation meet this rule?");
System.out.println("define an interval [k,j]");
System.out.print("please input the end postive integer k:");
int k = scanner.nextInt();
System.out.print("please input the end postive integerk j:");
int j = scanner.nextInt();
double m;
for (float y = k; y <= j; y++) {
for (float x = 1; x < y; x++) {
m = (y * (y + 1) / 2) * (y * (y + 1) / 2) - ((x - 1) * x / 2) * ((x - 1) * x / 2);
double c = Math.pow(m, 1.0 / 3);
BigDecimal d = new BigDecimal(c);
c = d.setScale(7, BigDecimal.ROUND_HALF_UP).doubleValue();
if (c == (int) c) {
System.out.println(" the first ipostive integer is:" + (int) x + " the last ipostive integer is:" + (int) y);
System.out.println("the true ipostive integer is:" + (int) c);
}
}
}
}
代码所在IDE编译器中运行结果如下图:
1→验证结果
`
2→查询代码
补充说明:
本文章的解题思路借鉴于https://blog.csdn.net/yue_zt/article/details/79694163,感谢学习的路上有各位大佬相伴!
Best Regards to you all !