Write a program that reads the total number of students, and each students name and score, and finally displays the name of the student with the lowest score and the average score.
Sample run:
Enter the number of students: 3
Enter student 1 name: John
Enter student 1 score: 71
Enter student 2 name: Chris
Enter student 2 score: 85.5
Enter student 3 name: Brad
Enter student 3 score: 94
Brad has the lowest score (71) and the average score is 83.5
i used if statement to solve this question
PHP Code:
import java.util.*;
public class HomeWork4 {
public static void main(String[] args) {
String student1,student2,student3;
double score1,score2,score3,average=0;
Scanner input=new Scanner(System.in);
System.out.println("Enter student 1 name: ");
student1=input.next();
System.out.println("Enter student 1 score: ");
score1=input.nextDouble();
System.out.println("Enter student 2 name: ");
student2=input.next();
System.out.println("Enter student 2 score: ");
score2=input.nextDouble();
System.out.println("Enter student 3 name: ");
student3=input.next();
System.out.println("Enter student 3 score: ");
score3=input.nextDouble();
average=(score1+score2+score3)/3.0;
if (score2>score3 && score3>score1 || score3>score2 && score2>score1)
System.out.println(student1+" has the lowerst score ("+score1+") and the average score is "+average);
else if (score3>score1 && score1>score2 || score1>score3 && score3>score2)
System.out.println(student2+" has the lowerst score ("+score2+") and the average score is "+average);
else if (score1>score2 && score2>score3 || score2>score1 && score1>score3)
System.out.println(student3+" has the lowerst score ("+score3+") and the average score is "+average);
System.exit(0);
but my teacher wants me to solve it using while method
PHP Code:
String name;
double numbers=0,score,studentsnum,average=0;
Scanner input=new Scanner(System.in);
System.out.println("Enter the numbers of students: ");
studentsnum=input.nextDouble();
while (numbers<=studentsnum)
System.out.println("Enter student "+(numbers++)+"name: ");
name=input.next();
System.out.println("Enter student "+(numbers++)+"score: ");
score=input.nextDouble();
i've tried my best but couldn't find a way to complete this thing
help is appreciated