2016-10-04 80 views
3

我在絕望需要與Java程序的幫助,我需要爲學校的競爭!我一直對這個至少3個小時過去3天,我不出來.... =(以下提供詳細路線:數學類,方法和隨機數(JAVA)

編寫調用每個數學的方法的程序從下面的列表類,提供顯示調用的方法輸出,發送到方法的值,返回的結果列出的每個方法會告訴你要使用的值,例如:

上市方法:

double pow(double a, double b): use 2.0 and 3.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的方法,該方法沒有參數並且不返回任何值。在這種方法中,執行以下操作:

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

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

℃。在循環後,顯示如下:

最小值:X 最大值:Y 平均位: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 

*/ 

所以,我遇到的問題是有關運行循環1000次後顯示的最大和最小數出1000個號碼的最後一部分。並顯示1,000個數字的均值。這裏是我的代碼

class MathMethodsProgram { 

public static void main(String[] args) { 
    //  Prints the pow method. 

    System.out.println("Math.pow(2.0, 3.0) = " + Math.pow(2.0, 3.0)); 

    //  Prints the sqrt method 

    System.out.println("Math.sqrt(25) = " + Math.sqrt(25)); 

    //  Prints the max number 

    System.out.println("Math.max(6, 3) = " + Math.max(6, 3)); 

    //  Prints the max number 

    System.out.println("Math.max(-50.0,7.0) = " + Math.max(-50.0, 7.0)); 

    //  Prints a random number 
    System.out.println("Math.random(); = " + Math.random());  

    randomStudy(); 

    } 

public static void randomStudy(){ 
    int total = 0; 
    int min = 11; 
    int max = -1; 
    int newtotal = 0; 
    int i; 
    int randomInt = 0; 

     for(i = 0; i < 1000; i++){    
      randomInt = 1 + (int)(Math.random() * 11); 

      newtotal = (randomInt + total); 

      } 
     if (randomInt < min) { 
      min = 1 + (int) Math.random(); 
     } else { 
      System.out.println("Min: " + min); 
     } 

     if (randomInt > max) { 
      max = 1 + (int) 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); 
    } 



} 

當我在Eclipse中運行代碼,這是輸出:提前

Math.pow(2.0, 3.0) = 8.0 
Math.sqrt(25) = 5.0 
Math.max(6, 3) = 6 
Math.max(-50.0,7.0) = 7.0 
Math.random(); = 0.1824716748725944 
Min value:1 
Max Value:1 
Average:0.008 

感謝

+1

問題是,您在檢查值之前關閉循環迭代! – Erik

+0

Furhtermore,你問'if(randomValue Turing85

+0

如果你小心格式化代碼,錯誤可能會變得明顯!你需要在每個循環的迭代中檢查並更新你的'min'和'max'。 –

回答

0

在你的1000環,你有

newtotal = (randomInt + total); 

哪個不增加newtotal,它只需要t他最後一個號碼。看起來你的最後一個數字是8,所以平均值是0.008。這將是更好的:

total = (randomInt + total); 
// Then after the for loop is complete, average = total/1000; 

接下來,randomInt與最小/最大比較,而不是分配一個新的隨機值,應先分配基礎上,最後一個隨機數的新的最小值/最大值(這需要在你的1000 for循環,不是之後):

if (randomInt > max) { 
    max = randomInt; 
} 

希望這有助於!

-1

您的代碼非常接近。

你只需要把你的if語句中的for循環,並在那裏你寫

max = 1 + (int) Math.random(); 

應改爲:

max = randomInt; 

(做相同分鐘,以便它讀取「分鐘= randomInt;「)

-1

這裏我將提供一個我創建的代碼。我的目的是爲您提供如何創建多個功能的想法,這些功能與我們從類獲得的功能相同。我收錄的評論幫助這些班級自我解釋,我希望這對你有所幫助。我還包括對話框來顯示數據,所以如果有人正在尋找幫助如何使用javax.swing類,它是一個很好的資源。

第一類MyMath.java包含用於執行所有計算的方法。

package com.exercise.lecture2; 

/** 
* Create a class call "MyMath" that has the following class methods: 
* static boolean isEven(int x) --> returns true is number is even 
* static long absolute(long x) --> returns the absolute value of x 
* static double hypotenuse (double x, double y) --> returns sqrt(x^2 + y^2) 
* static double max (double a, double b) --> returns max of a or b 
* static double min (double a, double b) --> returns min of a or b 
* 
* @author Samuel 
* 
*/ 
public class MyMath { 

    /** 
    * An even number is an integer which is "evenly divisible" by two. 
    * This means that if the integer is divided by 2, it yields no remainder. 
    * Zero is an even number because zero divided by two equals zero, which despite not being a natural number, is an integer. 
    * Even numbers can be either positive or negative. 
    * 
    * @param x 
    * @return 
    */ 
    static boolean isEven(int x){ 
     if (x % 2 == 0) 
      return true; 
     else 
      return false; 
    } 

    /** 
    * In mathematics, the absolute value or modulus of a real number is the number without the sign. 
    * The absolute value of 2 is 2, the absolute value of -2 is also 2. 
    * 
    * @param x 
    * @return 
    */ 
    static long absolute(long x) { 
     if (x >= 0) 
      return x; 
     else 
      return (-1*x); 
    } 

    /** 
    * The hypotenuse is the side opposite the 90 degrees angle in a right triangle. It is always the longest side. 
    * 
    * @param x 
    * @param y 
    * @return 
    */ 
    static double hypotenuse (double x, double y) { 
     return Math.sqrt((x * x) + (y * y)); 
    } 

    /** 
    * Maximum of two values 
    * @param a 
    * @param b 
    * @return 
    */ 
    static double max (double a, double b) { 
     if (a >= b) 
      return a; 
     else 
      return b;  
    } 

    /** 
    * Minimum of two values 
    * @param a 
    * @param b 
    * @return 
    */ 
    static double min (double a, double b) { 
     if (a < b) 
      return a; 
     else 
      return b; 
    } 

} 

第二類MathClassTest.java包含爲了顯示使用對話框的javax.swing類。

package com.exercise.lecture2; 

import javax.swing.JOptionPane; // importing the javax.swing class in order to use dialog box in my code 

/** 
* static boolean isEven(int x) --> returns true is number is even 
* static long absolute(long x) --> returns the absolute value of x 
* static double hypotenuse (double x, double y) --> returns sqrt(x^2 + y^2) 
* static double max (double a, double b) --> returns max of a or b 
* static double min (double a, double b) --> returns min of a or b 
* 
* Write an application that allows the user to select between the above methods and 
* returns the response to the user. 
* 
* @author Samuel 
* 
*/ 
public class MathClassTest { 

    public static void main(String[] args) { 

     /* used to test the functions on MyMath.java class, here is how all of this begin 
     * then I change it to make this tester use javax.swing class in order to display using 
     * dialog boxes. 
     * 
     // is Even 
     System.out.println(MyMath.isEven(-100)); 

     // Absolute value of an integer 
     System.out.println(MyMath.absolute(-10)); 

     // Hypotenuse of two values 
     System.out.println(MyMath.hypotenuse(3, 4)); 

     // Max of two values 
     System.out.println(MyMath.max(3, -4)); 

     // Min of two values 
     System.out.println(MyMath.min(3, -4)); 

     */ 

     boolean repeat; 
     do { 
      //String name = JOptionPane.showInputDialog(null, "Enter your name", "Enter name", JOptionPane.PLAIN_MESSAGE); 
      String option = JOptionPane.showInputDialog(null,"Choose 'a' or 'A' to determine if a number is EVEN or not. \n" // options 
        + "Choose 'b' or 'B' to determine the ABSOLUTE value of a number. \n" // the (+) sign concatenate the all of the followings Strings as one 
        + "Choose 'c' or 'C' to determine the HYPOTENUSE of two values. \n" 
        + "Choose 'd' or 'D' to determine the MAXIMUM of two values. \n" 
        + "Choose 'e' or 'E' to determine the MINIMUM of two values. \n" 
        + "Choose 'q' or 'Q' to quit this simple application. \n\n" 
        , "My Math class, by S. Mayol", JOptionPane.QUESTION_MESSAGE); // title bar of the dialog box 
      repeat = true; 

      if (option.equals("Q") || option.equals("q")) { 
       JOptionPane.showMessageDialog(null, "Thank you for participating...", "by Samuel Mayol", JOptionPane.INFORMATION_MESSAGE); 
       repeat = false; 
       System.exit(0); 
      } else { 
       if (option.equals("A")||option.equals("a")){ 
        try { // to prevent that the value inserted is an integer and not a letter 
         String val = JOptionPane.showInputDialog("Enter a number to deternine if the number is even"); 
         JOptionPane.showMessageDialog(null, "Is the number " + val + " even? " + MyMath.isEven(Integer.valueOf(val)), 
           "Even", JOptionPane.INFORMATION_MESSAGE); // add a title message on a dialog box 
         repeat = true; 
        } catch(Exception e){ 
         JOptionPane.showMessageDialog(null, "The input " + option + " is not valid integer, please try again", "WARNING!!!", 
           JOptionPane.WARNING_MESSAGE); 
        } 
       } 
       else if (option.equals("B")||option.equals("b")){ 
        try { 
         String val = JOptionPane.showInputDialog("Enter a number to calculate absolute value of that number"); 
         JOptionPane.showMessageDialog(null, "The absolute value of " + val + " is: " + MyMath.absolute(Long.valueOf(val)), 
           "Absolute", JOptionPane.INFORMATION_MESSAGE); 
         repeat = true; 
        } catch(Exception e){ 
         JOptionPane.showMessageDialog(null, "The input " + option + " is not valid integer, please try again", "WARNING!!!", 
           JOptionPane.WARNING_MESSAGE); 
        } 
       } 
       else if (option.equals("C")||option.equals("c")){ 
        try { 
         String val = JOptionPane.showInputDialog("Enter the first side to calculate the hypotenuse"); 
         String val2 = JOptionPane.showInputDialog("Enter the second side to calculate the hypotenuse"); 
         JOptionPane.showMessageDialog(null, "The hypotenuse of " + val + " and " + val2 + " is: " + MyMath.hypotenuse(Double.valueOf(val), Double.valueOf(val2)), 
           "Hypotenuse", JOptionPane.INFORMATION_MESSAGE); 
         repeat = true; 
        } catch(Exception e){ 
         JOptionPane.showMessageDialog(null, "The any of the inputs " + option + " are not valid integer, please try again", "WARNING!!!", 
           JOptionPane.WARNING_MESSAGE); 
        } 
       } 
       else if (option.equals("D")||option.equals("d")){ 
        try { 
         String val = JOptionPane.showInputDialog("Enter the first number to calculate the max value"); 
         String val2 = JOptionPane.showInputDialog("Enter the second number to calculate the max value"); 
         JOptionPane.showMessageDialog(null, "The max of " + val + " and " + val2 + " is: " + MyMath.max(Double.valueOf(val), Double.valueOf(val2)), 
           "Maximum", JOptionPane.INFORMATION_MESSAGE); 
         repeat = true; 
        } catch(Exception e){ 
         JOptionPane.showMessageDialog(null, "The any of the inputs " + option + " are not valid integer, please try again", "WARNING!!!", 
           JOptionPane.WARNING_MESSAGE); 
        } 
       } 
       else if (option.equals("E")||option.equals("e")){ 
        try { 
         String val = JOptionPane.showInputDialog("Enter the first number to calculate the min value"); 
         String val2 = JOptionPane.showInputDialog("Enter the second number to calculate the min value"); 
         JOptionPane.showMessageDialog(null, "The min of " + val + " and " + val2 + " is: " + MyMath.min(Double.valueOf(val), Double.valueOf(val2)), 
           "Minimum", JOptionPane.INFORMATION_MESSAGE); 
         repeat = true; 
        } catch(Exception e){ 
         JOptionPane.showMessageDialog(null, "The any of the inputs " + option + " are not valid integer, please try again", "WARNING!!!", 
           JOptionPane.WARNING_MESSAGE); 
        } 
       } 
       else 
        JOptionPane.showMessageDialog(null, "The input " + option + " is not valid, please try again", "A, B, C, D, E, or Q", 
         JOptionPane.WARNING_MESSAGE); 
      } 
     } while(repeat); 
    } 

}