Results 1 to 3 of 3
  1. #1
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default Algorithm problem

    This code is supposed to allow the user to enter a 5 student IDs and their results and calculate the total and show the grades all the information should be shown in a table. For some reason the first studentID is indented - does anyone know why?

    Java Code:
    import java.util.*;
     public class Assignment
     {
     	public static void main(String[] args)
     	{
     	//Declares variables and arrays
     	int i = 0;
     	int j = 0;
     	String [] studentIDArray = new String [5];
     	int [][] studentArray = new int [5][9];
     	Scanner kybd = new Scanner(System.in);
     
     	//Welcome message to user
     	System.out.println("Welcome");
     	System.out.println("~~~~~~~");
     	System.out.println("Please note any unanswered questions should marked as -1");
     	System.out.print ("\n");
     
     	//User is prompted to enter student ID and the results
     	for (i = 0; i < 5; ++i)
     		{
     		System.out.print("Please Enter the Student ID: ");
     		studentIDArray[i] = kybd.next();
     		for (j = 0; j < 8; ++j)
     			{
     			System.out.println("Please Enter the Result for Question " + (j+1));
     			studentArray[i][j] = kybd.nextInt();
     			}
     		}
     
     	//Calculates the students total
     	while (j < 8)
     		{
     		j = 0;
     		while (i < 5)
     			{
     			studentArray[i][9] += studentArray[i][j];
     			j+=1;
     			}
     		i+=1;
     		}
     
     	int k = 0;
     	String grade = "";
     	String [] gradeArray = new String [5];	//Declares new array to store grades
     
     	//Calculates the grades for each student
     	while (i < 5)
     		{
     		studentArray[i][8] += 3;
     		if (studentArray[i][8] > 70)
     			{
     			gradeArray[k] = "Distinction";
     			}
     	      		else if (studentArray[i][8] > 60)
     			{
            			gradeArray[k] = "Merit";
         			}
           			else if (studentArray[i][8] > 40)
     			{
     			gradeArray[k] = "Pass";
     			}
     			else if (studentArray[i][8] >= 40)
     			{
     			gradeArray[k] = "Fail";
     			}
     		k+=1;
     		i+=1;
     		}
     
     	System.out.print ("\n");
     	System.out.println ("StudentID Q1   Q2     Q3     Q4     Q5     Q6     Q7     Q8     Total   Grade");
     	System.out.print ("  ");
     
     	i=0;
     	j=0;
     	k=0;
     
     	//Output students results 
     	while (i < 5)
     	{
     		j = 0;
     		System.out.printf(studentIDArray[i]);
     		while (j < 9)
     		{
     			System.out.printf ("%7d", studentArray[i][j]);
     			j+=1;
     		}
     
     		System.out.print("     " + gradeArray[k]);
     		System.out.print ("\n");
     		i+=1;
     		k+=1;
     	}
     }
     }
    Marcus:cool:

  2. #2
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    Well, one thing at a time:

    Java Code:
    System.out.println ("StudentID Q1   Q2     Q3     Q4     Q5     Q6     Q7     Q8     Total   Grade");
     System.out.print ("  ");
    Why are you printing two spaces - without a newline after the header? It will simply add these two spaces to the next line - like it does.

    As far as the other issues - I'd strongly encourage you to add some System.out.prinln() statements here and there to understand what is going on.

    Felissa:p

  3. #3
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default

    Can I make a suggestion? - print out the value of "i" and "j" before you enter the while loops. It looks like you're incrementing but never resetting them.

    Eric

Similar Threads

  1. Problem with equation in my algorithm
    By romina in forum New To Java
    Replies: 2
    Last Post: 07-20-2007, 07:53 AM
  2. Help with algorithm
    By susan in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 10:26 PM
  3. Problem with algorithm
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-13-2007, 03:31 PM
  4. Help me with this algorithm
    By Marcus in forum Advanced Java
    Replies: 3
    Last Post: 07-02-2007, 01:30 PM
  5. Help with Algorithm
    By Daniel in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 05:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •