Results 1 to 9 of 9
Thread: for loop not working properly
- 10-15-2011, 12:39 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
for loop not working properly
I have an assignment to get user input for a students name, assignment, number score and letter grade. I have to use a for loop to determine how many students to enter information for and then output all of the information for each student. I cannot figure out where to put the line to output all of the information so that it doesn't output it after entering the information for each student.
Java Code:public class Grader { String last; String assign; int score; String letterGrade; public Grader(String lastName, String assignment, int grade, String letterGrade) { last = lastName; assign = assignment; score = grade; } public String getStudent() { return last; } public String getAssignment() { return assign; } public int getScore() { return score; } public String getGrade() { if (score <= 59) letterGrade = "F"; else if (score >= 60 && score <= 69) letterGrade = "D"; else if (score >= 70 && score <= 72) letterGrade = "C-"; else if (score >= 73 && score <= 76) letterGrade = "C"; else if (score >= 77 && score <= 79) letterGrade = "C+"; else if (score >= 80 && score <= 82) letterGrade = "B-"; else if (score >= 83 && score <= 86) letterGrade = "B"; else if (score >= 87 && score <= 89) letterGrade = "B+"; else if (score >= 90 && score <= 92) letterGrade = "A-"; else if (score >= 93 && score <= 96) letterGrade = "A"; else if (score >= 97 && score <= 100) letterGrade = "A+"; return letterGrade; } }
Java Code:import java.util.Scanner; public class GradeTester { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x = 0; System.out.print("Enter number of students: "); x = in.nextInt(); for (int i = 0; i < x; i++) { String letterGrade = ""; System.out.print("Enter the students last name: "); String lastName = in.next(); System.out.print("Enter the assignment: "); String assignment = in.next(); System.out.print("Enter the grade: "); int grade = in.nextInt(); Grader grade1 = new Grader(lastName, assignment, grade, letterGrade); System.out.print(grade1.getStudent() + grade1.getAssignment() + grade1.getScore() + grade1.getGrade()); } } }
-
Re: for loop not working properly
If you want to enter all the information for each student, and then after you're done doing this, display all the results, then you'll need two for loops, one to input the information and one after the first one to display the results.
- 10-15-2011, 12:47 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
Re: for loop not working properly
I added this for loop but now it is not displaying any information.
Java Code:for (int j = 0; i < j; j++) { Grader grade1 = new Grader(lastName, assignment, grade, letterGrade); System.out.print(grade1.getStudent() + grade1.getAssignment() + grade1.getScore() + grade1.getGrade()); }
-
Re: for loop not working properly
Sorry, I needed to look closer at your code. You'll need an array of Grader of size x, fill it with Grader objects in the first for loop, and then display the results held by the Grader items in the array in the second for loop. If you're still stuck, please post your latest code.
- 10-15-2011, 12:53 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
Re: for loop not working properly
I thought about using an array because that would make sense however we have not covered arrays yet so is there another way to do it without using an array?
-
Re: for loop not working properly
- 10-15-2011, 01:06 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
Re: for loop not working properly
The assignment says:
Ask the user for a student last name and a score with a Scanner using its next() method and its nextIn() method. It should then create a Grader object using the appropriate information calling the appropriate constructor. It should then display the information obtained from the Grader object and display the assignment name, student name, score, and grade. So is there any way to make this work without an array or would I have to display the information after each person is entered?
-
Re: for loop not working properly
My take on the assignment is that it looks like you will want to display the result of each student immediately after entering it. So that suggests that you use only one for loop, no array or ArrayList, and I think that the flow in pseudocode will be:
Java Code:for x number of students: enter one student's information display that same student's information end for loop.
- 10-15-2011, 01:33 AM #9
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
TCP/IP client not working properly
By Chrillz in forum NetworkingReplies: 2Last Post: 07-20-2011, 08:39 PM -
Why this block is not working properly?
By juhiswt in forum New To JavaReplies: 2Last Post: 03-13-2011, 12:08 PM -
date is not working properly
By newnewgen in forum New To JavaReplies: 1Last Post: 10-12-2010, 10:04 AM -
event handler not working properly
By H3rtaherta in forum Java 2DReplies: 3Last Post: 11-24-2008, 03:39 AM -
Log4j not working properly....
By prakash_dev in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 01:13 PM
Bookmarks