2014-11-25 6 views
0

Java 프로그램에서 ArrayList가 있습니다. 내가 뭘 원하는 인쇄는 예측할 수있는 경우 40 개 이상 될 것입니다 얼마나 많은 숫자 마크를 계산할 수Java ArrayList : 인쇄 메시지 용 ArrayList 활용

System.out.println = ("The amount of people that have more than 40 marks is " + x); 

'사람의 X 금액이 통과'말할 것이다 하단에있는 숫자입니다 ArrayList를 사용하여 넣은 표시의 양? 당신이 당신의 예에서 보여처럼

public class test { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 

    ArrayList<Integer> marks = new ArrayList<Integer>(); 

    Scanner input = new Scanner(System.in); 
    // Create a new scanner to use in java 

    int[] range = { 0,29,39,69,100 }; 
    // A new array is created with the grade boundaries 
    int[] inRange = new int[boundary.length - 1]; 
    // Indexed from 0 to n-1 

    int markIn; 
    // New integer markIn 
    do { 
    // This do-while loop calculates the expression after the statements below are exectued at least once 
     System.out.println("Enter Mark(s):"); 
     // Wait for user input 
     markIn = input.nextInt();  
     // markInp value is set as the value entered by user 
     marks.add(markIn); 

     for (int a=1 ; a<boundary.length ; a++) 
     // for loop will take the variable 'a' and compare it with varibale 'boundary', when the condition is satisfied that value of 'a' increments by 1 
      if (range[a-1] <= markInp && markInp <= range[a]) { 
      // The boundaries will define the upper and lower limits of the markInp 
       inRange[a-1]++; 
       //inRange is incremented by 1 
       break; 
       //Exit if 
      } 
    } while (markIn <= 100); 
    // When the mark exceeds 100, the loop is stopped 

    System.out.println(marks); 

    input.close(); 
} // Close the Scanner input 
} 
+0

그래서 정확히 무엇이 문제입니까? – Mureinik

+0

내 질문은; ArrayList를 사용하여 불확실한 표시가있는 경우 40 개가 넘는 표시가 몇 개나되는지 계산할 수 있습니까? –

답변

0

당신은 같은 것을 수행 할 수 있습니다

int result = 0; 

if(marks != null) 
{ 
    Collections.sort(marks); 
    for(int i=0;i<marks.size();i++) 
    { 
     if(marks.get(i) > 40) 
     { 
      result = marks.size() - i; 
      break; 
     } 
    } 
} 

마크은 출력을 요구하는 ArrayList를하고 결과입니다.

0

하면 배열은 이미 그때 당신은 당신이 다음 배열의 길이를 복용하고 위치를 뺀, 특정 점수를보고 시작하는 곳에서 간단한 검색을 할 필요가, 분류 된 경우 귀하의 검색에서 나온 요소의

배열이 정렬되지 않은 경우 배열을 정렬 한 다음 검색을 수행하십시오.

+0

배열은 콘솔을 통해 어떤 양의 마크 입력을 가질 수 있습니다. 예를 들어, 배열 요소를 39보다 크고 100보다 작은 요소와 비교할 방법이 없을까요? –

+0

@ nmehta_001은 입력이 입력되기 전에 이미 알려진 경계입니까? 그렇다면 입력을 생성 할 때마다 해당 목록에 입력을 추가하고 배열 대신 집합을 사용하여 비교할 수 있습니다. – CBredlow