2016-12-15 67 views
3

我想學習Java;這裏是我正在努力的練習:小編碼挑戰(費馬大定理)

費馬最後定理說沒有整數a,b和c使得a^n + b^n = c^n,除非n = 2.

編寫一個名爲checkFermat的方法,它將四個整數作爲參數a,b,c和n,並檢查Fermat定理是否成立。如果n大於2,結果是a^n + b^n = c^n,那麼程序應該打印出「神聖煙霧,Fermat是錯誤的!」否則程序會打印出「不, t工作「。

您應該假設有一個名爲raiseToPow的方法,它將兩個整數作爲參數,並引發第二個參數的第一個參數。例如: int x = raiseToPow(2, 3); 將值8賦給x,因爲2^3 = 8

我也遇到一些問題,例如我似乎無法使用Math.Pow(a, n)int,只能用double。如果你有興趣,這裏是我迄今爲止,隨意跳過它,只是在答案中寫你自己的版本的程序。

(請記住,我開始這本書只幾天就回來。)

package fermat.s_last_theorem; 
    import java.lang.Math; 
    import java.util.Scanner; 

    public class FermatS_Last_Theorem { 


    public static void main(String[] args) { 

    Scanner s = new Scanner (System.in); 
    System.out.println("Inster First Number"); 
    double frst = s.nextDouble(); 
    System.out.println("Insert Second Number"); 
    double scnd = s.nextDouble(); 

    System.out.println("Insert Exponent"); 
    double expo = s.nextDouble(); 


    double v = FLaw(frst,scnd,expo); 
    double k = FLawRes(v, expo); 

    System.out.println("The answer is " + v); 
    System.out.println("Your answer rooted by your exponent is " + k); 
    Law(v, Pow(k, expo)); 


    } 

    public static double Pow(double a, double b) { 
    double res = Math.pow (a, b); 
    return (res); 
    } 

    public static double FLaw(double frst, double scnd, double expo) { 
    double D1 = Pow(frst, expo); 
    double D2 = Pow(scnd, expo); 


    return (D1 + D2); 

    } 

    public static double FLawRes(double res, double base) { 

    double D3 = Pow(res, 1/base); 
    return D3; 
     } 

    public static void Law(double v, double k) { 
     if (v==k) { 
    System.out.println("Pythagora works."); 
     } else { 
    System.out.println("Pythagora doesnt work"); 
    } 
    } 
} 

的主要問題是,我不完全知道如何回答練習要求的問題,並計劃上市上面的內容不能正常工作。

+0

你可以使用'(int)Math.Pow(a,n)'將返回值轉換回整數。 (當然,假設您沒有超出整數的有效範圍。) – AJNeufeld

+0

您正在尋找「類型轉換」的概念。 – Victory

+0

您可以閱讀Java原始類型轉換,類型轉換和精度。 –

回答

3

你應該假設,有一個名爲raiseToPow方法...

這意味着你編寫使用這種方法你的代碼,即使你沒有方法。您的代碼將被手動審查,或者教師可能會提供該方法並運行您的代碼。

如果你想測試你的代碼,你總是可以自己實現它。在開啓代碼之前,您應該刪除該方法。

但這裏的意圖是,這是一個寫在紙上的練習。


現在,如何實施int raiseToPow(int a, int b)

想一下它的含義。 3 意味着3 * 3 * 3 * 3。

因此,實施乘以a本身的方法b次。

我會把它作爲你的另一個練習。

1

你可以打破它是這樣的:通過調用此方法使用不同的參數

public boolean checkFermat(int a, int b, int c, int n) { 
    if(n != 2 && 
     (checkFermatCondition(a,b,c,n) || 
     checkFermatCondition(a,c,b,n) || 
     checkFermatCondition(b,c,a,n))) { 
     System.out.println("Holy smokes, Fermat was wrong!"); 
    } else { 
     System.out.println("No, that doesn’t work."); 
    } 
} 

在這種方法中,您只是想降低您查詢條件的所有組合的

private boolean checkFermatCondition(int a, int b, int c, int n) { 
    return raiseToPow(a,n)+raiseToPow(b,n) == raiseToPow(c,n); 
} 
0

您的函數raiseToPow()的功能可以使用Math.pow

import java.util.Scanner; 

class Main { 
    public static void main(String[] args) { 
    Scanner s = new Scanner(System.in); 
    System.out.println("Fermat's Last Theorem: a^n+b^n != c^n (n!=2)"); 
    int a, b, c, n; 
    System.out.print("Enter value for a:"); 
    a = s.nextInt(); 
    System.out.print("Enter value for b:"); 
    b = s.nextInt(); 
    System.out.print("Enter value for c:"); 
    c = s.nextInt(); 
    while(true){ 
     System.out.print("Enter value for n:"); 
     n = s.nextInt(); 
     if(n!=2) 
     break; 
     System.out.println("n cannot be 2"); 
    } 
    checkFremat(a,b,c,n); 
    } 

    public static void checkFremat(int a, int b, int c, int n){ 
    if ((int)Math.pow(a, n)+(int)Math.pow(b, n)!=(int)Math.pow(c, n)) 
     System.out.println("Fermat was correct!"); 
    else 
     System.out.println("Holy smokes, Fermat was wrong!"); 
    } 
} 

試試吧here!