
Patrick B. answered 07/08/21
Math and computer tutor/teacher
You say JAVA but then you say printf....
printf is C/C++ , not Java.
//** JAVA ****//
import java.util.Scanner;
class GradeAvg
{
public static void main(String args[])
{
(new GradeAvg()).Go();
}
public void Go()
{
String name;
int N=3;
double testScores[] = new double[N];
double testScoreAvg;
Scanner scanner = new Scanner(System.in);
System.out.print("Please input name :>");
name = scanner.nextLine();
double testScoreSum=0;
for (int iLoop=0; iLoop<3; iLoop++)
{
System.out.print(" Please input test score # " + (iLoop+1) + " :>");
testScores[iLoop] = scanner.nextDouble();
testScoreSum += testScores[iLoop];
}
System.out.println( name + " grades are:");
for (int iLoop=0; iLoop<N; iLoop++)
{
System.out.println(testScores[iLoop]);
}
testScoreAvg = testScoreSum/N;
System.out.println(" Average grade = " + testScoreAvg);
}
}
//C
#include <stdio.h>
#define NUM_TEST_SCORES (3)
Go()
{
char name[255];
int iLoop;
double testScores[NUM_TEST_SCORES];
printf(" Please input name :>");
scanf("%s",name);
double testScoreSum=0;
for (iLoop=0; iLoop<NUM_TEST_SCORES; iLoop++)
{
printf(" Please input test score # :>");
scanf("%lf",&testScores[iLoop]);
testScoreSum += testScores[iLoop];
}
printf(" %s grades are: \n",name);
for (iLoop=0; iLoop<NUM_TEST_SCORES; iLoop++)
{
printf("%6.2f \n",testScores[iLoop]);
}
double testScoreAvg = testScoreSum/NUM_TEST_SCORES;
printf(" average is %6.1f \n ",testScoreAvg);
}
main()
{
Go();
}