用户定义的类

时间:2016-10-31 05:54:47

标签: java arrays class user-defined-functions class-method

我的代码发布在下面。我生成随机数字等级并为其分配字母等级。然后将创建一个客户端类来测试该程序。我的问题是该计划的结束。我收到一个错误的错误表达。也许我只需要一双新的眼睛来看待这个。我似乎无法弄明白。谢谢!

import java.util.Random;

public class Convertgrade
{
     private int numberOfStudents;

     private int[] studentGrades = new int[100];


     public Convertgrade()
     {
            numberOfStudents = 0;
     }

      //one arg constructor, sets numberOfStudents = students
      public Convertgrade(int students)

      {
             numberOfStudents = students;
             setGrades(students);
      }

      //mutator
      public void setStudents(int students)
      {
            if(students < 0)
            {
                       numberOfStudents = 0;
             }
             else
            {
                       numberOfStudents = students;
                       setGrades(students);
              }
      }

        //accessor
       public int getStudents()

       {
       return numberOfStudents;
        }

       public void setGrades(int students)
       {
              Random randomNumber = new Random(); 
              for(int count = 0; count < students; count++)
              {
                    studentGrades[count] = randomNumber.nextInt(101);
                    }
               }

          public char[] getLetterGrades()
          {
                   char[] letterGrades = new char[numberOfStudents];
                   for(int count = 0; count < numberOfStudents; count++)
                   {
                           if(studentGrades[count] <= 59)
                           letterGrades[count] = 'F';

                           else if(studentGrades[count] <= 69)
                           letterGrades[count] = 'D';

                           else if(studentGrades[count] <= 79)
                           letterGrades[count] = 'C';

                           else if(studentGrades[count] <= 89)
                           letterGrades[count] = 'B';

                           else if(studentGrades[count] <= 100)
                           letterGrades[count] = 'A';
                            }
                       return letterGrades;
                       }

           public int getAs()

           {

                      int numberOfAs = 0;

                      for(int count = 0; count < numberOfStudents; count++)

                      {

                                if(studentGrades[count] >= 90)

                                numberOfAs++;

                       }
                  return numberOfAs;
             }

           public int[] getNumberOfEachGrade()

          {

                 int[] numberOfGrades = new int[numberOfStudents];

                 for(int count = 0; count < numberOfStudents; count++)

                  {

                        if(studentGrades[count] <= 59)

                        numberOfGrades[count] ++;

                        else if(studentGrades[count] <= 69)

                        numberOfGrades[count] ++;

                        else if(studentGrades[count] <= 79)

                        numberOfGrades[count] ++;

                        else if(studentGrades[count] <= 89)

                        numberOfGrades[count] ++;



                        else if(studentGrades[count] <= 100)

                        numberOfGrades[count] ++;

                 }

              return numberOfGrades;

             }



            public String toString()

            {

                  System.out.format("The number of students = %d\n", numberOfStudents);

                  for(int count = 0; count < numberOfStudents; count++)

                  {

                           System.out.format("Grade %d is %d\n", count + 1, studentGrades[count]);

                  }

            return String.format("\n");

             public void sort_array(int[] array); 
             //GETTING ERRORS HERE^: ILLEGAL START OF EXPRESSION

            {

                    int intTemp;

                    int count = 0;

                    int index = 0;

                    for(count = 0; count < (array.length - 1); count++)

                    {

                         for(index = 0; index < (array.length - 1); index++)

                         {

                                if(array[index] > array[index +1])

                                {

                                        intTemp = array[index +1];

                                        array[index +1] = array[index];

                                        array[index] = intTemp;

                                    }

                           }

                     }

               }

      }

}

3 个答案:

答案 0 :(得分:2)

我强烈建议您使用IDE,例如EclipseIntelliJ。或者任何可以帮助您维护健康代码样式的东西,例如Sublime Text和一些Java代码样式包。

一切都会做!

你有3个问题:

  1. 缺少右括号(})第149行(大致)
  2. 额外的分号行150和
  3. 文件末尾有太多括号。
  4. 这次我为你清理了你的代码,但这只是输入问题,希望你能找到一种方法来避免将来:)

    import java.util.Random;
    
    public class Convertgrade {
    
        private int numberOfStudents;
        private final int[] studentGrades = new int[100];
    
        public Convertgrade() {
            this.numberOfStudents = 0;
        }
    
        // one arg constructor, sets numberOfStudents = students
        public Convertgrade(final int students) {
            this.numberOfStudents = students;
            setGrades(students);
        }
    
        // mutator
        public void setStudents(final int students) {
            if (students < 0) {
                this.numberOfStudents = 0;
            } else {
                this.numberOfStudents = students;
                setGrades(students);
            }
        }
    
        // accessor
        public int getStudents() {
            return this.numberOfStudents;
        }
    
        public void setGrades(final int students) {
            final Random randomNumber = new Random();
            for (int count = 0; count < students; count++) {
                this.studentGrades[count] = randomNumber.nextInt(101);
            }
        }
    
        public char[] getLetterGrades() {
            final char[] letterGrades = new char[this.numberOfStudents];
            for (int count = 0; count < this.numberOfStudents; count++) {
                if (this.studentGrades[count] <= 59) {
                    letterGrades[count] = 'F';
                } else if (this.studentGrades[count] <= 69) {
                    letterGrades[count] = 'D';
                } else if (this.studentGrades[count] <= 79) {
                    letterGrades[count] = 'C';
                } else if (this.studentGrades[count] <= 89) {
                    letterGrades[count] = 'B';
                } else if (this.studentGrades[count] <= 100) {
                    letterGrades[count] = 'A';
                }
            }
    
            return letterGrades;
        }
    
        public int getAs() {
            int numberOfAs = 0;
            for (int count = 0; count < this.numberOfStudents; count++) {
                if (this.studentGrades[count] >= 90) {
                    numberOfAs++;
                }
            }
    
            return numberOfAs;
        }
    
        public int[] getNumberOfEachGrade() {
            final int[] numberOfGrades = new int[this.numberOfStudents];
            for (int count = 0; count < this.numberOfStudents; count++) {
                if (this.studentGrades[count] <= 59) {
                    numberOfGrades[count]++;
                } else if (this.studentGrades[count] <= 69) {
                    numberOfGrades[count]++;
                } else if (this.studentGrades[count] <= 79) {
                    numberOfGrades[count]++;
                } else if (this.studentGrades[count] <= 89) {
                    numberOfGrades[count]++;
                } else if (this.studentGrades[count] <= 100) {
                    numberOfGrades[count]++;
                }
            }
    
            return numberOfGrades;
    
        }
    
        @Override
        public String toString() {
            System.out.format("The number of students = %d\n", this.numberOfStudents);
            for (int count = 0; count < this.numberOfStudents; count++) {
                System.out.format("Grade %d is %d\n", count + 1, this.studentGrades[count]);
            }
    
            return String.format("\n");
        }
    
        public void sort_array(final int[] array) {
            int intTemp;
            int count = 0;
            int index = 0;
            for (count = 0; count < (array.length - 1); count++) {
                for (index = 0; index < (array.length - 1); index++) {
                    if (array[index] > array[index + 1]) {
                        intTemp = array[index + 1];
                        array[index + 1] = array[index];
                        array[index] = intTemp;
                    }
                }
            }
        }
    }
    

答案 1 :(得分:0)

你在错误的地方有一些大括号{}和分号。

你的缩进看起来有些混乱 - 也许这只是你在发布这篇文章时如何复制代码,但是小心缩进可能会使这些错误变得更加明显。

  • 在'public void sort_array(int [] array)'后面有一个分号,它不应该在那里
  • 在此
  • 之前,你有一个缺失的右括号}
  • 文件末尾有一个额外的右括号

答案 2 :(得分:0)

替换此部分:

        return String.format("\n");

         public void sort_array(int[] array); 
         //GETTING ERRORS HERE^: ILLEGAL START OF EXPRESSION

由此:

    return String.format("\n");
}

public void sort_array(int[] array) {
    // OMG THERE ISN'T ERRORS ANYMORE :)

如果你看到这样的“回归”陈述悬而未决,下次你可以说出一些奇怪的事情......