我编写了一个代码,需要检查一个理论,即除非n=2 a^n + b^n = c^n
,否则以下公式永远不会成立。
该程序最后告诉我“Meh,Fermat是对的”标志着程序的结束,但之后它抛出了很多异常。
这是代码 -
public class CheckFermats {
public static void main(String[] args) {
int a=2;
int b=2;
int c=2;
int n=3;
int round = 2;
CheckupA (a,b,c,n,round);
}
public static void CheckupA (int a, int b, int c, int n ,int round){
if (Math.pow(a,n)+Math.pow(b,n)==Math.pow(c, n))
{
System.out.println("HOLYSHIT FERMET WAS WRONG!!");
}
else
{
if (a==9)
{
a=round+1;
CheckupB (a,b,c,n, round);
}
else
{
a++;
CheckupA (a,b,c,n, round);
}
}
}
public static void CheckupB (int a, int b, int c, int n,int round){
if (Math.pow(a,n)+Math.pow(b,n)==Math.pow(c, n))
{
System.out.println("HOLYSHIT FERMET WAS WRONG!!");
}
else
{
if (b==9)
{
b=round+1;
CheckupC (a,b,c,n, round);
}
else
{
b++;
CheckupB (a,b,c,n, round);
}
}
}
public static void CheckupC (int a, int b, int c, int n,int round){
if (Math.pow(a,n)+Math.pow(b,n)==Math.pow(c, n))
{
System.out.println("HOLYSHIT FERMET WAS WRONG!!");
}
else
{
if (c==9)
{
c=round+1;
round++;
CheckIfFinished (n);
checkIfIncreaseN (a,b,c,round, n);
CheckupA (a,b,c,n, round);
}
else
{
c++;
CheckupC (a,b,c,n, round);
}
}
}
public static void checkIfIncreaseN(int a, int b, int c, int round, int n){
if(round==9)
{
round = 2;
a=2;
b=2;
c=2;
n++;
CheckupA (a,b,c,n,round);
}
}
public static void CheckIfFinished (int n){
if (n==9)
{
System.out.println("Meh, Fermet was right");
return;
}
}
}
答案 0 :(得分:0)
即使您已检查Meh, Fermet was right
,您的代码也会继续。查看代码CheckIfFinished (n);
,即使它已完成,也会继续。
所以你可以在方法中添加一个变量来检查是否满足了检查,或者使用方法的返回值。
E.g。附加参数:
public static void CheckupC (int a, int b, int c, int n,int round, boolean finished)
{
if (finished) {return;}
...
}
或者您使用方法的返回值,以表明它已完成。
if (CheckupC(...)) { return true; }
当你将它用于整个代码时,它看起来像这样(也许它不是最好的风格,但它解决了你的问题):
public static void main(String[] args) {
int a=2;
int b=2;
int c=2;
int n=3;
int round = 2;
CheckupA (a,b,c,n,round);
}
public static boolean CheckupA (int a, int b, int c, int n ,int round)
{
if (Math.pow(a,n)+Math.pow(b,n)==Math.pow(c, n))
{
System.out.println("HOLYSHIT FERMET WAS WRONG!!");
return true;
}
else
{
if (a==9)
{
a=round+1;
return CheckupB (a,b,c,n, round);
}
else
{
a++;
return CheckupA (a,b,c,n, round);
}
}
}
public static boolean CheckupB (int a, int b, int c, int n,int round)
{
if (Math.pow(a,n)+Math.pow(b,n)==Math.pow(c, n))
{
System.out.println("HOLYSHIT FERMET WAS WRONG!!");
return true;
}
else
{
if (b==9)
{
b=round+1;
return CheckupC (a,b,c,n, round);
}
else
{
b++;
return CheckupB (a,b,c,n, round);
}
}
}
public static boolean CheckupC (int a, int b, int c, int n,int round)
{
if (Math.pow(a,n)+Math.pow(b,n)==Math.pow(c, n))
{
System.out.println("HOLYSHIT FERMET WAS WRONG!!");
return true;
}
else
{
if (c==9)
{
c=round+1;
round++;
if (CheckIfFinished (n)) {
return true;
}
if (checkIfIncreaseN (a,b,c,round, n)) {
return true;
}
return CheckupA (a,b,c,n, round);
}
else
{
c++;
return CheckupC (a,b,c,n, round);
}
}
}
public static boolean checkIfIncreaseN(int a, int b, int c, int round, int n)
{
if(round==9)
{
round = 2;
a=2;
b=2;
c=2;
n++;
return CheckupA (a,b,c,n,round);
}
return false;
}
public static boolean CheckIfFinished (int n)
{
if (n==9)
{
System.out.println("Meh, Fermet was right");
return true;
}
return false;
}
使用double
进行计算时,还有一些建议不要将双值与==
进行比较,而是使用Double.compare
。