Vote count:
0
// First enter the total number of student. Then prompt input to ask user for N number of name and mark for student. Display the name from a to z accordingly and the respective mark.
public static void main(String[] args) {
Scanner scannum= new Scanner (System.in);
Scanner scanstr= new Scanner (System.in);
System.out.print("Enter total number of student: "); //total number of student
int totalstu= scannum.nextInt(); //input student
String [] name = new String[totalstu]; // declare student name
int [] mark = new int[totalstu]; // declare student mark
for(int i=0; i<totalstu; i++){ // loop of number of student
System.out.print("Enter student " +(i+1)+ "'s name: "); // input name
name[i] = scanstr.nextLine();
System.out.print ("Enter student "+(i+1)+"'s mark: "); // input mark
mark[i] = scannum.nextInt();
}
System.out.println("\n\t********** Programming in Java **********\t\n"); //print
System.out.println("No.\tName\t\t\tMarks\tGrade"); //print
sort(name,mark); //call
for(int i=0; i<totalstu; i++){
System.out.print((i+1)+"\t");
System.out.print(name[i]+"\t\t\t");
System.out.print(mark[i]+"\t");
if (mark[i]>=90)
System.out.println("A+");
else if (mark[i]>=80)
System.out.println("A");
else if (mark[i]>=75)
System.out.println("A-");
else if (mark[i]>=70)
System.out.println("B+");
else if (mark[i]>=65)
System.out.println("B");
else if (mark[i]>=60)
System.out.println("B-");
else if (mark[i]>=55)
System.out.println("B-");
else if (mark[i]>=50)
System.out.println("C");
else if (mark[i]>=45)
System.out.println("C-");
else if (mark[i]>=40)
System.out.println("D");
else
System.out.println("F");
}
}
private static void sort(String [] name,int []mark) {
String temp1;
int temp;
for (int i = 1; i <= name.length - 1; i++) { //push the z to [4] and a to [0]
int currentName = 0;
for (int j = 0; j < (name.length - i); j++) {
if (name[j].compareTo( name[j + 1])>0) //if strArray[j] > strArray[j+1], swap the elements
{
temp1 = name[j];
name[j] = name[j + 1];
name[j + 1] = temp1;
currentName = j;
}
}
if (currentName != i) {
temp = mark[currentName];
mark[currentName] = mark[i];
mark[i] = temp;
}
}
}
asked 1 min ago
Aucun commentaire:
Enregistrer un commentaire