我可以在两个连续的if语句后执行for循环吗?

时间:2018-04-19 09:44:39

标签: java for-loop if-statement

我正在开发一个必须检查某些条件的程序(如果没有执行则发出错误消息),然后再执行两个for循环(一个用于输入的成绩总和,一个用于最高和最低成绩)而且我不确定如何对它们进行排序。

另外我的代码给我这个错误 - >线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0并且我不知道如何解决错误

如果有人能帮我解决正确序列的骷髅,我会非常感激!

这是我的代码(我知道它看起来很疯狂,抱歉我正在学习!):

import java.util.Scanner;

public class Homework {

    public static void main (String[] args) {
        Scanner input = new Scanner(System.in);
        int numberOfMarks, average, sum, total, mark;
        numberOfMarks = 0;
        int [] marks = new int[numberOfMarks];
        int smallest, largest;
        //System.out.println("Enter number of marks: ");
        numberOfMarks = input.nextInt();
        if(numberOfMarks<0) {
        System.out.println("Number of marks must be greater than 0!!");
        }
        //System.out.println("Enter "+numberOfMarks+"marks: ");
        mark = input.nextInt();
        if(mark<0) {
            System.out.println("Negative marks not allowed!!!");
        } else if(mark>100) {
            System.out.println("Marks above 100% not allowed!!!");
        }  
        sum = 0;
        for(int i = 0; i < marks.length; i++) {
            sum += marks[i];
        }
        smallest=marks[0];
        largest=marks[0];
        for(int i=1;i<marks.length;i++) {       
            if(marks[i]>largest) {
                largest=marks[i];
            } else if(marks[i]<smallest) {
                smallest=marks[i];
            }
            average = sum/numberOfMarks;
            System.out.println("Highest Mark = "+largest);
            System.out.println("Lowest Mark = "+smallest);
            System.out.println("Average = "+average);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

问题在于逻辑,请参阅下面的评论

import java.util.Scanner;

public class Homework {

 public static void main(String[] args) {

  Scanner input = new Scanner(System.in);

  int numberOfMarks, average, sum, total, mark;
  numberOfMarks = 0;
  /*there you are creating empty array
    you told marks should have length of value numberOfMarks, but 
    numberOfMarks is equal to zero at this time
  */
  int[] marks = new int[numberOfMarks]; 
  int smallest, largest;

  //System.out.println("Enter number of marks: ");
  numberOfMarks = input.nextInt();

 /*
   not interesting conditions checking... 
 */

  for (int i = 0; i < marks.length; i++) {
   /*
     There you are trying to access 1st element of marks
       - indexing from zero, as usual
     But there is no first element, array has zero length, zero elements
      Ok, you told array should have length of numberOfMarks, but it was 
       when value was zero, so if you will change numberOfMarks later, it 
        has no impact on the length of array
   */
   sum += marks[i];
  }

  /*
    stats computing, not important
  */

 }

}

因此,一旦您获得了用户输入的标记数量,那么您可以“重新创建” - 重新分配/重新定义数组,如前所述 marks = new int[numberOfMarks];

在你的情况下

//System.out.println("Enter number of marks: ");
numberOfMarks = input.nextInt();

在数组长度中使用变量并不意味着它将是有界的,因此如果数组长度变量(在这种情况下为numberOfMarks)将在使用后更改,那么数组的长度仍将是相同的

答案 1 :(得分:0)

你可以试试这个:

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int numberOfSubjects, sum;
        numberOfSubjects = 0;

        System.out.print("Enter the total number of subjects: ");
        numberOfSubjects = in.nextInt();

        if(numberOfSubjects < 0){
        System.out.println("Number of marks must be greater than 0!!");
        }
        else {
            int []marks = new int[numberOfSubjects];
            sum = 0;

            System.out.print("Enter the marks: ");
            // add condition inside for loop for marks not less than 1 or greater than 100 
            for(int i=0; i<marks.length; i++) {
                    marks[i] = in.nextInt();
            }

            // calculate total
            for(int i = 0; i < marks.length; i++){
                sum += marks[i];
            }

            // sort marks
            Arrays.sort(marks);

            System.out.println("Total: " + sum);
            System.out.println("Average: " + sum/numberOfSubjects);
            System.out.println("Minimum: " + marks[0]);
            System.out.println("Maximum: " + marks[marks.length-1]);
        }
    }
}

代码流应该如下所示。即使您可以在阅读标记时添加标记条件(不小于1或大于100)。