I am currently taking a computer science class in high school, and it is my first time programming (2 weeks since I started). I have currently finished my program; however, there were a few details that bothered me. I was supposed to create a program which can calculate average of individual marks and then calculate all of the average marks previously entered by user.
However, I noticed something that bothered me, which was the fact that if I were to enter a number when asked to enter a name, it would assume that number is a name. Also, when I asked for the 5 marks, the program would run fine until an error occurs if I accidentally type in a letter or word. I have done some research and found system scanners; however, I still don't understand the concept in a looping situation.
import java.io.*;
public class loopingEx6Final {
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double average, totalaverage = 0;
int Mark, Marktotal=0, marktotalsum=0, count=0;
String strName, strMark;
System.out.println("This program will calculate an individual's personal average and then calculate the class average whn instructed.");
System.out.println("Please type (finish) in order to calculate class average.");
System.out.println("Please enter a name.");
strName = br.readLine ();
while (!strName.equals("finish")){
System.out.println("The follwing will calculate the average five marks of "+ strName +".");
System.out.println("Please enter 5 marks.");
count++;
for (int i=0;i<5;i++) {
Mark = Integer.parseInt(br.readLine());
Marktotal= Marktotal + Mark;
}
average = Marktotal/5;
System.out.println(strName+"'s average is "+average);
Marktotal= 0;
System.out.println("\n"+"Please enter a name.");
strName = br.readLine ();
totalaverage=(totalaverage+average)/count;
}
System.out.println("The class average of all input grades is:");
System.out.println(totalaverage);
System.out.println();
System.out.println("Thank you for using the program.");
}
}