简体   繁体   中英

Showing multiple answers in the output using arrays for java

While I was writing the code for displaying the scores below a certain value, it only showed one of the integer that suits the condition while there were multiple of them. For example the user was to type a integer ranging from 1 to 100 and the scores below 65 should have been displayed, but even though there are 64 and 45, it would only show 45. How am I suppose to show all of the values that suits the condition? Here is my code I've came up

 import java.util.Scanner;
 import java.util.ArrayList;
 public class Tester {

public static void main(String[] args) {
    
    Scanner scan = new Scanner (System.in);
    ArrayList <Integer> gradeArray = new ArrayList<Integer>();
    System.out.print("Enter the number of courses you completed : ");
    int course = scan.nextInt();
    int highg=0;
    
    int lowg=0;

    for(int i=1;gradeArray.size()<course;i--){
        
        System.out.print("Enter the grade recieved : ");
        int grade = scan.nextInt();
        
        if (grade<=100 && grade >= 0) {
            gradeArray.add(grade);
            
            if(grade > 95) 
                highg = grade;
                
             if (60 > grade)
                lowg = grade;
        }
    }
    
    System.out.println("Grades qualified for the honors award : " + highg);
    
    System.out.println("Grades that are below passing :" + lowg );
        
    }
}

Actually you are writing only the last grades stored in highg and lowg . Try writing a loop over the gradeArray variable.

If needed you can also try to store the high and low values in a separate list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM