2014-12-22 53 views
-4

我一直在試圖完成這個Java項目,併爲了我的生活,我無法讓它運行,我一直在想我的大腦。下面是我應該做的事:初學者的Java - 需要使用Eclipse的幫助

  1. 編寫調用每一個從下面的列表Math類的方法,並提供輸出顯示的方法調用,發送到方法值的程序,返回結果。每個列出的方法都會告訴您使用哪些值。例如:

    列出的方法:double pow(double a, double b):使用2.03.0。您的程序將顯示:Math.pow(2.0, 3.0) = 8.0。當調用接受雙精度的方法時,即使它爲零,也應使用至少有一位十進制數字的數字,如上例所示。請記住帶小數的數字是雙字。嘗試使用會產生易於驗證的結果的值。

    下面的列表:

    double pow(double a, double b): Use 3.0 and 2.0 
    double sqrt(double x): Use 25.0 
    int max(int a, int b): Use 6 and 2 
    double max(double a, double b): Use -50.0 and 7.0 
    static double random()

    示例輸出:

    Math.pow(3.0, 2.0) = 9.0 
    Math.sqrt(25.0)=5.0 
    Math.max(6,2)=6 
    Math.max(-50.0,7.0)=7.0 
    Math.random()= -random value-

  • 測試你與所示的值的程序示例,修復任何錯誤。

  • 向程序中添加一個名爲randomStudy的方法,該方法沒有參數並且不返回任何值。在此方法中,執行以下操作:

    a。聲明三個int變量:total,min和max。將總數設置爲0,最小值爲11,最大值爲-1。

    b。創建一個將運行1000次的循環。在循環體中,生成一個介於1和10之間的隨機int值。將此數字添加到您的總數。如果此數字小於最小值,則用新數字更新最小值。如果它大於最大值,則用新數字更新最大值。

    c。循環後,顯示以下內容:

    Min value: x 
    Max value: y 
    Average: z

    用您的最小值和最大值替換x和y。計算z的總數除以1000d。

  • 從主要方法調用您的新randomStudy方法。

  • 突出顯示並複製Eclipse中顯示的輸出。將輸出粘貼到源代碼文件的底部。在文本結果上方添加文本結果,然後註釋掉剛剛打開的文本以及輸出文本。您的評論結果應該是這個樣子:

    /* 
    Results: 
    Math.pow(3.0, 2.0)= 9.0 
    Math.sqrt(25.0) = 5.0 
    Math.max(6, 2) = 6 
    Math.max(-50.0, 7.0) = 7.0 
    Math.random() = -random number- 
    Min value: 1 
    Max value: 10 
    Average: 5.553 
    */

  • public class Method { 
    
        public void main(String[] args) { 
    
    public static double pow(double 3.0, double 2.0); 
        public static double sqrt(double 25); 
        public static int max(int 6, int 2); 
        public static double max(double -50, double 7) 
        public static double random() 
        } 
    } 
    
    public class randomStudy{ 
    
        public void main(String[] args) { 
         int min = 11; 
           int max = -1; 
           int total = 0; 
        int i; 
         for(i = 0; i < 1000; i++){ 
          int randomInt = (int)(11.0 * Math.random()); 
           System.out.println("Random integer between 1 and 10 : " + randomInt); 
           int newtotal = (randomInt + int total); 
           if int newtotal < int min { 
            int min = Math.random()); 
           } 
           else{ 
            System.out.println("int min"); 
           } 
           if int newtotal > max { 
            int max = Math.random()); 
           } 
           else{ 
            System.out.println("int max"); 
           } 
         } 
         System.out.println("Min value:" + int min); 
         System.out.println("Max Value:" + int max); 
         System.out.println("Average:"+ int newtotal/1000d); 
        } 
    } 
    

    我得到這個錯誤

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
        The method pow(double, double) is undefined for the type Math 
        The method pow(double, double) is undefined for the type Math 
        at Math.main(Math.java:3) 
    
    +0

    這是徹頭徹尾的題外話,完全不理什麼程序員是** [講述](http://programmers.stackexchange.com/tour) **:http://meta.stackexchange.com/a/129632/165773 – gnat

    +2

    我建議你閱讀[如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug -small-programs /) – Doval

    +2

    我建議你閱讀教科書的第二和第三章。你並沒有真正走上正軌。 – Evorlor

    回答

    0

    您的代碼不正確的結構。你有多個主要方法,這是一個大問題。特別是對於這個程序應該只有一個。您聲明已經存在的變量:

    int variableA = 0; 
    int variableA; // Overrides old variableA thus resetting the value 
    

    相反,你應該這樣做是爲了使用變量:

    int variableA = 0; 
    variableA = 10; 
    

    您還聲明中,你不能這樣做的主要方法的方法。這也是你錯誤的原因。

    public static double pow(double 3.0, double 2.0); 
    public static double sqrt(double 25); 
    public static int max(int 6, int 2); 
    public static double max(double -50, double 7) 
    public static double random() 
    

    你也當你聲明一個數據類型,如「雙重」你寫的下一件事是變量名,而不是價值不能寫「雙3.0」。在嘗試繼續之前,請仔細閱讀Java和編程基礎知識。

    下面你會發現你的代碼被修復和重新格式化。然而,我不是在爲你做你的工作,所以你將不得不弄清楚如何自己完成並通過實例學習。

    'Math.java'

    public class Math { 
        public static void main(String[] args) { 
         // Program starts here 
    
         // Show and calculate math results here 
         System.out.println("Math.pow(3.0, 2.0) = " + Math.pow(3.0, 2.0)); 
         // ... 
    
         // This will execute the randomStudy method. 
         randomStudy(); 
        } 
    
        public void randomStudy() { 
         // randomStudy method code goes here 
         int min = 11; 
         int max = -1; 
         int total = 0; 
    
         for(int i = 0; i < 1000; i++) { 
          int randomInt = (int)(11.0 * Math.random()); 
    
          System.out.println("Random integer between 1 and 10: " + randomInt); 
    
          int newtotal = (randomInt + total); 
    
          if (newtotal < min) { 
           min = Math.random()); 
          } else { 
           System.out.println("Min: " + min); 
          } 
    
          if (newtotal > max) { 
           max = Math.random()); 
          } else { 
           System.out.println("Max: " + max); 
          } 
         } 
    
         System.out.println("Min value:" + min); 
         System.out.println("Max Value:" + max); 
         System.out.println("Average:"+ newtotal/1000d); 
        } 
    } 
    
    +0

    這很好,我設法自己弄清楚了這一點,當我回頭查看時發現了這一點,所以我認爲這是我確實弄清楚的確認。這次真是萬分感謝! – YMDigital

    +0

    另外當你這樣做:「public void randomStudy()」我需要將它改爲:「public static void randomStudy()」,否則它將不會執行主方法中的randomStudy。 – YMDigital