import java.util.Scanner;
public class Ex2 {
public static void main(String[] args) {
/*
Write a program to display, in the form of bar graph, the number of students in a class that scored "Approved" and "Failed" on a set of subjects.
The program should start by asking the number of students in the class and the number of subjects and, for each subject will ask the name of the subject and the number of students with approval.
The output should look like this:
Subject: Math
- Approved: *******
- Failed: ***
*/
Scanner sc = new Scanner(System.in);
int numberOfStudents = sc.nextInt();
sc.nextLine(); //buffer clean
int quantityOfClasses = sc.nextInt();
sc.nextLine(); //buffer clean
int approvedStudents;
String subject;
while(quantityOfClasses > 0){
subject = sc.next();
approvedStudents = sc.nextInt();
sc.nextLine(); //buffer clean
studentsOverallStatus(numberOfStudents, approvedStudents, subject);
quantityOfClasses--;
}
}
public static void studentsOverallStatus(int numberOfStudents, int approvedStudents, String subject){
int failedStudents = numberOfStudents - approvedStudents;
System.out.printf("%nSubject: %s%n", subject);
System.out.print("- Approved: ");
while (approvedStudents > 0) {
System.out.print("*");
approvedStudents--;
}
System.out.printf("%n- Failed: ");
while (failedStudents > 0) {
System.out.print("*");
failedStudents--;
}
}
}
import java.util.Scanner;
public class Ex2 {
public static void main(String[] args) {
/*
Write a program to display, in the form of bar graph, the number of students in a class that scored "Approved" and "Failed" on a set of subjects.
The program should start by asking the number of students in the class and the number of subjects and, for each subject will ask the name of the subject and the number of students with approval.
The output should look like this:
Subject: Math
- Approved: *******
- Failed: ***
*/
Scanner sc = new Scanner(System.in);
int numberOfStudents = sc.nextInt();
sc.nextLine(); //buffer clean
int quantityOfClasses = sc.nextInt();
sc.nextLine(); //buffer clean
int approvedStudents;
String subject;
while(quantityOfClasses > 0){
subject = sc.next();
approvedStudents = sc.nextInt();
sc.nextLine(); //buffer clean
studentsOverallStatus(numberOfStudents, approvedStudents, subject);
quantityOfClasses--;
}
}
public static void studentsOverallStatus(int numberOfStudents, int approvedStudents, String subject){
int failedStudents = numberOfStudents - approvedStudents;
System.out.printf("%nSubject: %s%n", subject);
System.out.print("- Approved: ");
while (approvedStudents > 0) {
System.out.print("*");
approvedStudents--;
}
System.out.printf("%n- Failed: ");
while (failedStudents > 0) {
System.out.print("*");
failedStudents--;
}
}
}