[go: up one dir, main page]

0% found this document useful (0 votes)
14 views1 page

Unit3Assignment Question4 PDF

方法

Uploaded by

easonshi686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Unit3Assignment Question4 PDF

方法

Uploaded by

easonshi686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

package JAVA;

import java.util.Scanner;

public class Assignment4 {


public static void main(String[] args) {
// Create a Scanner object for reading input
Scanner scanner = new Scanner(System.in);
// Prompt to enter the number of students
System.out.print("Enter num of students: ");
int numStudents = scanner.nextInt();
// Initialize total height, tallest and shortest height
double totalHeight = 0, tallest = Double.MIN_VALUE, shortest = Double.MAX_VALUE;

for (int i = 1; i <= numStudents; i++) {


double height;
// Ensure the height is non-negative
do {
System.out.print("Enter student " + i + " height: ");
height = scanner.nextDouble();
if (height < 0) {
System.out.println("Invalid height, re-enter!");
}
} while (height < 0);

// Accumulate the height, update the tallest and shortest height


totalHeight += height;
tallest = Math.max(tallest, height);
shortest = Math.min(shortest, height);
}

// Calculate and print the average height, tallest height and shortest height
double average = totalHeight / numStudents;
System.out.println("Average height: " + average);
System.out.println("Tallest height: " + tallest);
System.out.println("Shortest height: " + shortest);

// Close the Scanner


scanner.close();
}
}

You might also like