ARRAYs - Display number of students who failed
Here is a practise revision question that im stuck on:
Q32) Write a program that reads in 10 student marks into an array called marks
and computes the number of students who have failed (got a mark less than 40).
Ok so here is what i have so far:
public static void main(String[] args) {
int[] marks = new int[10];
String strMark = "";
int Marksread = 0;
for (Marksread = 0; Marksread < marks.length; Marksread++){
strMark = JOptionPane.showInputDialog("Enter a Mark:");
marks[Marksread] = Integer.parseInt(strMark);
I have managed to read in the 10 marks but i cant figure out how to display the number of student that have failed. I know i have to use a loop to count it up but just cant seem to get my head round it and keep getting errors :( Need help please... :s
Re: ARRAYs - Display number of students who failed
Quote:
I know i have to use a loop
Add code inside the loop to test if the grade is a fail and if it is, add one to a counter.
Quote:
keep getting errors
Please post the full text of the error messages.
Re: ARRAYs - Display number of students who failed
Hello roronaz!
Quote:
Originally Posted by
roronoaz
I know i have to use a loop to count it up but just cant seem to get my head round it and keep getting errors :( Need help please... :s
Please post the full text of the error messages.
If you know how to loop through an array which part is giving you trouble? Keep a int variable which will hold the number of students who failed. Then you just have to loop through the array and check if each of the array\'s elements is less than 40: if it is, you increment the counter.
Hint: harcode an array for testing and when you make it work, then use JOptionPane.
And please use the code tags to get highlighting in your code.
Re: ARRAYs - Display number of students who failed
Okay so i've tried using the loop but i keep getting '1' as a result everytime i run it :(
public static void main(String[] args) {
int[] marks = new int[10];
String strMark = "";
int count = 0;
int Marksread = 0;
for (Marksread = 0; Marksread < marks.length; Marksread++){
strMark = JOptionPane.showInputDialog("Enter a Mark:");
marks[Marksread] = Integer.parseInt(strMark);
}
if (Marksread<40){
count++;
}
System.out.println(count + " Students have failed!");
}
}
the result:
run:
1 Students have failed!
BUILD SUCCESSFUL (total time: 16 seconds)
Re: ARRAYs - Display number of students who failed
You need to check the value of the array in Marksread position (ie marks[Marksread]), not Marksread itself.
Re: ARRAYs - Display number of students who failed
Thank you Andreas! I've finally got it working now :)
public static void main(String[] args) {
int[] marks = new int[10];
String strMark = "";
int count = 0;
int Marksread = 0;
for (Marksread = 0; Marksread < marks.length; Marksread++){
strMark = JOptionPane.showInputDialog("Enter a Mark:");
marks[Marksread] = Integer.parseInt(strMark);
if (marks[Marksread]<40){
count++;
}
}
System.out.println(count + " Students have failed!");
}}
Re: ARRAYs - Display number of students who failed
Glad you got it work. Make sure you mark the thread as solved (from the Thread Tools in the top of the thread).