Posted on October 18th, 2009 at 8:29 PM by Brian(admin)



This is a Java program that takes input from the user for 3 student names, and 5 test scores for each student.  This program uses a “for” loop for the student name input, and another “for” loop for the score input.  This program uses “if..else” statements to place the names and scores into variables, and uses “System.out.printf” to output these into a table format.  This could be done easier with arrays, but this is a beginning Java program, before I have learned how to use arrays or different classes, so this is the best way I know how.


//Brian Meyer
//Test
import java.util.Scanner;

public class gradeInputProgram
{
public static void main(String[] args)
{
String student1=new String();
String student2=new String();
String student3=new String();
String inputName;
int inputScore;
int name;
int score;
int total1=0;
int total2=0;
int total3=0;
Scanner keyboard = new Scanner (System.in);

for (name=1;name<=3;name++)
{
System.out.print(“Enter the name for student number ” + name + “: “);
inputName = keyboard.nextLine();
if (name==1)
{
student1 =  inputName;
}
else if (name==2)
{
student2 =  inputName;
}
else
{
student3 =  inputName;
}
System.out.println(“”);
for (score=1;score<6;score++)
{
System.out.print(“Enter score number ” + score + “: “);
inputScore=keyboard.nextInt();
if(name==1)
{
total1 =total1+inputScore;
}
else if(name==2)
{
total2 =total2+inputScore;
}
else
{
total3 =total3+inputScore;
}
keyboard.nextLine();

}
System.out.println(“”);

}
System.out.println(“”);
System.out.printf(“%-15s %-6s \n”,”Student”,”Score”);
System.out.println(“*********************”);
System.out.printf(“%-15s %-6d \n”,student1,total1);
System.out.printf(“%-15s %-6d \n”,student2,total2);
System.out.printf(“%-15s %-6d \n”,student3,total3);
}
}

Leave a Reply

Anti-Spam Protection by WP-SpamFree

Get Adobe Flash playerPlugin by wpburn.com wordpress themes