-2

私はJavaプログラミングが初めてです。これは宿題の一部です。配列に対して一連の比較メソッドを提供する必要があります。私は最善を尽くしましたが、これがこれまでの私の試みです。誰かが私を啓発できますか?どんな助けでも大歓迎です!

私が従う必要があるいくつかの要件:

  • Selector クラスにパブリック メソッドを追加しないでください。適切と思われるプライベート メソッドを自由に追加できますが、パブリック メソッドを追加することはできません。
  • Selector クラスには、パブリックまたはプライベートのいずれのフィールドも追加しないでください。
  • java.util.Arrays 以外のものをインポートしてはいけませんが、これをインポートする必要はまったくありません。
  • 最も近い方法と最も遠い方法でのみ、ソリューションの一部として並べ替えを使用できます。
  • 既存のコンストラクターを変更したり、他のコンストラクターを追加したりしないでください。このクラスは、厳密に静的メソッドのプロバイダーとして設計されており、インスタンス化しないでください。


import java.util.Arrays;

/**
* A class that contains various selection methods on arrays.
* All methods assume that the array is completely filled with
* non-null values. If this is not true, the behavior is not specified.
* All the methods throw an IllegalArgumentException if the array
* parameter is null or has zero length. The array parameter to
* each method is guaranteed to not be changed as a result of the call.
*/
 public final class Comparision{


 /**
  * C A N N O T   I N S T A N T I A T E   T H I S   C L A S S .
  *
  */
  private Comparision(){    
  }
  /**
  * Return the element of a nearest to val. This method throws an
  * IllegalArgumentException if a is null or has zero-length.
  * The array a is not changed as a result of calling this method.
  *
  * @param a the array to be searched
  * @param val the reference value
  * @return the element a[i] such that ABS(a[i] - val) is minimum
  *
  */
  public static int nearest(int[] a, int val) {
     int[] a = new int[10];
     if (a == null || a.length == 0){
       throw new IllegalArgumentException("a is null or has zero-length");
     }
     int idx = 0;
     int distance = Math.abs(a[0]-val);
     for(int c = 1; c< a.length; c++){
         int cdistance = Math.abs(a[c] - val);
         if(cdistance < distance){
             idx=c;
             distance = cdistance;
         }
     }
     int theNumber = a[idx];
     return theNumber;
  }

  /**
   * Return the element of a farthest from val. This method throws an
   * IllegalArgumentException if a is null or has zero-length.
   * The array a is not changed as a result of calling this method.
   *
   * @param a the array to be searched
   * @param val the reference value
   * @return the element a[i] such that ABS(a[i] - val) is maximum
   *
   */
  public static int farthest(int[] a, int val) {
     int[] a = new int[10];
     if (a == null || a.length == 0){
       throw new IllegalArgumentException("a is null or has zero-length");
     }
     int idx = 0;
     int distance = Math.abs(a[0]-val);
     for(int c = 1; c< a.length; c++){
       int cdistance = Math.abs(a[c] - val);
       if(cdistance > distance){
        idx=c;
        distance = cdistance;
       }
     }
     int theNumber = a[idx];
     return theNumber;
  }

  /**
   * Return the k elements of a nearest to val.
   * The array a is not changed as a result of calling this method.
   * This method throws an IllegalArgumentException if k is negative.
   * This method returns an array of zero length if k == 0 or if
   * k > a.length.
   *
   * @param a the array to be searched
   * @param val the reference value
   * @param k the number of near elements to identify
   * @return the k elements a[i] such that ABS(a[i] - val)
   * are the k smallest evaluations
   *
   */
  public static int[] nearestK(int[] a, int val, int k) {
     int[] b = new int[10];
     for (int i = 0; i < b.length; i++){
       b[i] = Math.abs(a[i] - val);
     }
     Arrays.sort(b);
     int[] c = new int[w];
     w = 0;
     for (int i = 0; i < k; i++){
       if (k < 0){
       throw new IllegalArgumentException("k is not invalid!");
       }
       c[w] = b[i]; 
       w++;  
     }
  return c;   
  }

  /**
   * Return the k elements of a farthest from val.
   * The array a is not changed as a result of calling this method.
   * This method throws an IllegalArgumentException if k is negative.
   * This method returns an array of zero length if k == 0 or if
   * k > a.length.
   *
   * @param a the array to be searched
   * @param val the reference value
   * @param k the number of far elements to identify
   * @return the k elements a[i] such that ABS(a[i] - val)
   * are the k largest evaluations
   *
   */
  public static int[] farthestK(int[] a, int val, int k) {
     int[] b = new int[10];
     for (int i = 0; i < 10; i++){
       b[i] = Math.abs(a[i] - val);
     }
     Arrays.sort(b);
     int[] c = new int[w];
     int w = 0;
     for (int i = array.length-1; i >= array.length-k; i--){
       if (k < 0){
       throw new IllegalArgumentException("k is not invalid!");
       }
       else if (k == 0 || k > a.length){
       int[] c = "";
       }
       c[w] = b[i];
       w++;   
     }
  return c;    
  }

  /**
   * Return the number of elements of a that are greater than val.
   *
   * @param a the array to be searched
   * @param val the reference value
   * @return the number of elements a[i] such that a[i] > val
   *
   */
  public static int numGreater(int[] a, int val) {
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)>0){
       w++;
     }

  return w;
  }

  /**
   * Return an array of all the elements of a that are greater than val.
   * If a contains no elements greater than val, this method returns an
   * array of zero length.
   *
   * @param a the array to be searched
   * @param val the reference value
   * @return the elements a[i] such that a[i] > val
   *
   */
  public static int[] greater(int[] a, int val){
     int[] b = new int[w];
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)>0){
       b[w] = a[i];
       w++;
       }
     }
     if (w = 0){
     int[] b = {};
     }
  return b;
  }

  /**
   * Return the number of elements of a that are less than val.
   *
   * @param a the array to be searched
   * @param val the reference value
   * @return the number of elements a[i] such that a[i] < val
   *
   */
  public static int numLess(int[] a, int val) {
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)<0){
       w++;
     }
  return w;
  }

  /**
   * Return an array of all the elements of a that are less than val.
   * If a contains no elements less than val, this method returns an
   * array of zero length.
   *
   * @param a the array to be searched
   * @param val the reference value
   * @return the elements a[i] such that a[i] < val
   *
   */
  public static int[] less(int[] a, int val) {
     int[] b = new int[w];
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)<0){
       b[w] = a[i];
       w++;
       }
     }
     if (w = 0){
     int[] b = {};
     }
  return b;
  }
}
4

2 に答える 2

3

問題は 194 ~ 197 行にあります。if ステートメントを閉じていないため、その後の閉じ中括弧はすべてめちゃくちゃです。これを修正するには、numLess を次のように変更します。

public static int numLess(int[] a, int val) {
    int w = 0;
    for (int i = 0; i < a.length; i++){
        if ((a[i] - val)<0){
            w++;
        } // This is the curly brace you are missing
    }
    return w;
}

編集:他の答えも正しいです。numGreater と numLess の両方で同じ問題があります。両方の関数に中括弧を追加すると、正しくコンパイルされます。

于 2013-09-01T23:39:52.603 に答える
2

括弧またはセミコロンが欠落していると思います:

これは次のようになります。

  public static int numGreater(int[] a, int val) {
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)>0){
       w++;
     }

  return w;
  }

次のようにする必要があります。

  public static int numGreater(int[] a, int val) {
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)>0){
           w++;
       }//<---missing this fella
     }

     return w;
  }
于 2013-09-01T23:39:41.097 に答える