2014-10-24 2 views
1

저는 초급 Java 프로그래머입니다 (예 : 레벨 0 ...). 이 프로젝트가 진행 중이지만 며칠 동안 곤란을 겪었습니다. 나는 또한 내가 알지 못했던 많은 실수를했을 것이다.입력 배열 요소의 평균/최고/최저 등급 찾기

사용자가 그 경계를 넘지 못하게 100.00로 0.00에서 등급의 일련의 입력하도록 요청 :

이 프로젝트는 이것이다. 최대 100 개의 성적을위한 충분한 공간을 확보하십시오. 사용자가 -1을 입력하면 성적 추가가 중단됩니다. 성적의 평균을 계산하는 (generic!) 함수를 만듭니다. 또한 가장 높은 점수를 받고 가장 낮은 점수를 얻는 함수를 만듭니다. 이 기능을 사용하여 평균, 최고 및 최저 성적을 인쇄하여 사용자에게 알려줍니다.

지금까지 코드의 첫 번째 절반 (최대 100 개의 성적 입력)을 받았지만 평균, 최고 및 최저 성적을 찾는 방법에 대해서는 완전히 단서가 없습니다. 누군가 나에게 아이디어를 주거나 적어도 저에게 어떤 종류의 모범을 줄 수 있습니까?

감사합니다.

여기 순간으로 내 코드입니다 (그것은 특히 평균/최고/가장 낮은 등급의 주위에, 불완전) :

import java.util.*; 

public class GradeStats 
{ 
/* 
*Set the array size to 100 
*keep track of where the user is in the array 
* 

*/ 

/* 
* Ask the user to enter a series of grades from 0.00 to 100.00. Do not let the user exceed those boundaries. Make enough space for up to 100 grades. 
* Stop adding grades when the user enters -1. 
* Create a function to compute the average of the grades. Also create functions to get the highest and lowest of the grades. 
* Use these functions to print out the average, highest, and lowest grades. 
*/ 

public static void main(String [] args) 
{ 
    // Program loop checker 
    boolean done = false; 

    // Two doubles used in the equation 
    double d1; // Remember we can use commas 

    // Scanner to get user input 
    Scanner inputReader = new Scanner(System.in); 

    // Goal of the program 
    System.out.println("\nThis program will find the average, highest, and lowest grades.");  

    // Set up an array of 100 empty slots 
    int age[] = new int[100]; 

    // Program instruction for the user 
    System.out.println("Enter a series of grades from 0.00 to 100.00 or type '-1' to exit.\n"); 

    while(!done) 
    { 
     // Input grade 

     double gradeA = inputReader.nextDouble(); 
     Scanner endReader = new Scanner (System.in); 

     if(gradeA == -1) 
     { 
       done = true; 
     } 
     else if(gradeA > 100) 
     { 
       done = true; 
       System.out.println("Sorry, this is not a valid grade."); 
     } 
    } 

    int gradeCount; //Number of input grades 
    double grade; //Grade Value 

    while (gradeCount! = -1) 
    { 
     gradeCount = gradeCount++ 
    } 

    if (gradeCount !=0) 
    { 
     double average 
     average = double total/ gradeCount; 
     System.out.println("The average grade of the students is " + average(grade)); 
    } 

    // Return the average of an array of integers 
     public static double arrayAverage(int intArray[]) 
     { 

     double arrayAverage; 
     arrayAverage = gradeA/3; 
     return averageAnswer; 
    } 
     if (hGrade > lGrade) 
     { 
     System.out.println("The highest grade is" +hGrade); 
     } 

     else if (lGrade < hGrade) 
     { 
      System.out.println("The grade is +hGrade); 
     } 

     System.out.println("The lowest grade is" + lGrade); 
     System.out.println("The average grade of the students is " + arrayAverage(grade)); 
    // Say bye bye 
     System.out.println("Bye."); 
    } 
} 
+0

당신은'hGrade'와'lGrade'가 있습니다. 어디에서 왔으며, 그 밖의 무엇을 사용하고 있습니까? – Fodder

답변

0

Denise의 답을 바탕으로 double 배열을이 함수에 전달하여 평균, 최소 및 최대를 인쇄 할 수 있습니다.

private void printAverageMinAndMax(double[] grades){ 
    double total = 0.0; 
    double max = 0.0; 
    double min = Double.MAX_VALUE; 
    //Loop through all of the grades. 
    for(int i = 0; i < 100; i++){ 
     double grade = grades[i]; 
     //Add the grade to the total 
     total += grade; 
     //If this is the highest grade we've encountered, set as the max. 
     if(max < grade){ 
      max = grade; 
     } 
     //If this is the lowest grade we've encountered, set as min. 
     if(min > grade){ 
      min = grade; 
     } 
    } 

    System.out.println("Average is: " + (total/100)); 
    System.out.println("Max is: " + max); 
    System.out.println("Min is: " + min); 
} 
+0

최저 학년 (분)이 학년보다 큰 이유는 무엇입니까? 왜 min이 최대 값입니까? – joykyuu

+0

'min' 변수를 가능한 가장 높은 double 값으로 초기화합니다. 그런 다음 배열의 각 '등급'에 대해 '최소'변수보다 작은 지 확인합니다. 그렇다면 'min'을 'grade'로 설정합니다. 따라서 루프의 끝까지 'min'은 배열의 가장 낮은 'grade'와 같습니다. – hooda

1

당신은 어딘가에 등급 (gradeA)를 저장해야합니다. 이 질문은 사용자가 입력하려고하는 성적 집합의 평균을 계산하도록 요청하지만 단 하나만 얻었습니다. 당신이 배열에 삽입, 루프를 통과하면서 그런

double[] grades = new double[100]; 

: 당신은 단지 100 등급을위한 충분한 공간이 가정하고 있기 때문에 당신이 할 수있는

한 가지, 배열을 만드는 것입니다 :

int index = 0; 
while(!done) 
{ 
    // Input grade 

    double gradeA = inputReader.nextDouble(); 
    Scanner endReader = new Scanner (System.in); 

    if(gradeA == -1) 
    { 
      done = true; 
    } 
    else if(gradeA > 100) 
    { 
      done = true; 
      System.out.println("Sorry, this is not a valid grade."); 
    } 
    grades[index] = gradeA; 

    index++; 
} 

그런 다음 정수 배열을 평균, 최소, 최대 함수에 전달할 수 있습니다. (어느 쪽이 시작했는지 알 수 있습니다.)