Tuesday, July 28, 2009

Arrays in java, 1 week on and still stuck?

I have to Write a program which takes a series of 10 grades that students have gained for a coursework exercise (characters taken from A, B, C, D, E or F), typed in by the user. It should keep a count of how many of each grade the students achieved. These counts should be stored in an array of size 6, with cell 0 of the array holding the total number of As, cell 1 holding the total number of Bs, cell 2 holding the number of Cs etc.





I can do all the arrays and everything but sorting is a problem, If you look at the code you will see where i am stuck.





import javax.swing.JOptionPane;





public class Unit10Dem2


{


public static void main(String[] args)





{


String[]grades=new String[6];


String input;





int count=1;


for (count=1; count%26lt;=10; count++)


{





input=JOptionPane.showInputDialog("Plea... enter a students grade");


if (input=="a")


{


grades[0]=new String("a");


}


System.out.println(grades[count]);


}


}


}





Any advice please?

Arrays in java, 1 week on and still stuck?
String grade;


int[] numGrades = new int[6];





final String[] possibleGrades = {


"A", "B", "C", "D", "E", "F"


};


boolean isValidGrade;





for(int i = 0; i %26lt; 10 i ++){


isValidGrade = false;


while(isValidGrade == false){ // prompt user until valid input detected





// prompt user for string here and store in variable called grade





for(int j = 0; j %26lt; possibleGrades.length; j++){


if(grade.equalsIgnoreCase(possibleGrad...


isValidGrade = true;


break;


}


}





if(grade.equalsIgnoreCase("A")){


numGrades[0] += 1;


}else if(grade.equalsIgnoreCase("B")){


numGrades[1] += 1;


}else if(grade.equalsIgnoreCase("C")){


numGrades[2] += 1;


}else if(grade.equalsIgnoreCase("D")){


numGrades[3] += 1;


}else if(grade.equalsIgnoreCase("E")){


numGrades[4] += 1;


}else if(grade.equalsIgnoreCase("F")){


numGrades[5] += 1;


}else{


System.out.println("Invalid grade entered: " + grade);


}


}





// print results


System.out.println("Number of As: " + numGrades[0]);


System.out.println("Number of Bs: " + numGrades[1]);


System.out.println("Number of Cs: " + numGrades[2]);


System.out.println("Number of Ds: " + numGrades[3]);


System.out.println("Number of Es: " + numGrades[4]);


System.out.println("Number of Fs: " + numGrades[5]);

wildflower

No comments:

Post a Comment