【问题标题】:error: illegal start of expression; reached end of file while parsing错误:表达式的非法开始;解析时到达文件末尾
【发布时间】:2013-09-01 23:31:09
【问题描述】:

我是 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;
  }
}

【问题讨论】:

  • 检查numGreater的实现。我认为您缺少一些花括号。
  • 您确实应该能够解决自己的语法错误。至少我期待它。
  • 您使用的是 IDE 吗?蚀?网豆? IDE 会快速突出显示这些语法错误,以便您轻松发现并修复它们。
  • 我正在使用 jGRASP。谢谢!我真的应该在来这里之前摆脱语法错误。

标签: java throw


【解决方案1】:

你的问题在第 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;
}

EDIT:另一个答案也是正确的;你在 numGreater 和 numLess 中都有同样的问题。在两个函数中添加大括号,它应该可以正确编译。

【讨论】:

    【解决方案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;
      }
    

    【讨论】:

      猜你喜欢
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多