Results 1 to 3 of 3
Thread: Algorithm problem
- 07-01-2007, 01:29 AM #1
Member
- Join Date
- Jun 2007
- Posts
- 92
- Rep Power
- 0
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?
Marcus:cool: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; } } }
- 07-01-2007, 01:33 AM #2
Member
- Join Date
- Jun 2007
- Posts
- 95
- Rep Power
- 0
Well, one thing at a time:
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.Java Code:System.out.println ("StudentID Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Total Grade"); System.out.print (" ");
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
- 07-01-2007, 01:37 AM #3
Senior Member
- Join Date
- Jun 2007
- Posts
- 111
- Rep Power
- 0
Similar Threads
-
Problem with equation in my algorithm
By romina in forum New To JavaReplies: 2Last Post: 07-20-2007, 07:53 AM -
Help with algorithm
By susan in forum New To JavaReplies: 1Last Post: 07-13-2007, 10:26 PM -
Problem with algorithm
By Albert in forum AWT / SwingReplies: 1Last Post: 07-13-2007, 03:31 PM -
Help me with this algorithm
By Marcus in forum Advanced JavaReplies: 3Last Post: 07-02-2007, 01:30 PM -
Help with Algorithm
By Daniel in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 05:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks