简体   繁体   中英

2-D Array Assignment in Java

Doing a small assignment in Java:

You're grading ten assignments for a class of 20. Write a piece of code that creates and initializes an appropriate array. For the purpose of this exercise, you can assign them all the same points. You realized that you had made a mistake in your grading and need to credit 0.5 point to everyone for assignment 7. Write appropriate segment to do this. Print the grades for each student for each assignment. Your output should include student id, assignment number, and corresponding points.

Here's what I have so far:

public static void main (String [] args)
{
    float[][] grades = new float[20][10];

    for (int student=0; student<20; student++)
    {
        for (int assignment=0; assignment<10; assignment++)
        {
            grades[student][assignment] = (float)6.0;
        }

        for (int assignment=6; assignment<10; assignment++);
        {
            grades[student][6] += 0.5;
        }   

I understand that I need to do another double for loop to finish the sequence to eventually output the three values, but I'm not sure exactly what since I can't do the original double loop again.

credit 0.5 point to everyone for assignment 7

This

for (int assignment = 6; assignment < 10; assignment++); {
    grades[student][6] += 0.5;
}

Isn't going to behave like what the assignment asks you to do. Assuming this is inside first for loop, although you're missing a } ) and the indentation is weird; this will change student's 7th (index 6) grade 10 - 6 = 4 times, which will add (10 - 6) * 0.5 = 2 points to every student, but the assignment is asking to add only 0.5 . You should remove mentioned for loop, since it's not needed, keep just the body - logic where you're adding points.

Your output should include student id, assignment number, and corresponding points.

If I understand this, you have to print following line out, for every student's each assignment:

{student id} {assignment id} {assignment points}

To do this, after the line which you should now have fixed, add the following:

for (int assignment=0; assignment<10; assignment++) {
    System.out.printf("%d %d %d", student, assignment, grades[student][assignment]);
}

The loop is almost identical to one where you're adding the score, but the problem is that you have to fix assignment number 7 before printing out the results, so you can't just do the printing in that same loop.

I'd like to make a few suggestions for you to use (or not) as you wish:

  1. Make all the numbers (10, 20) constants. The standard form is private static final int ASSIGNMENT_COUNT = 10;
  2. Make each of the separate steps (assign, adjust, output) separate methods. This will make it clear that they are separate steps.
  3. Use 6.0F for the float constant. This makes it clear that it's a float and removes the need for a cast.
  4. In your for loops compare your iteration variable to array.length - that is safer and makes it clearer what you are trying to do.

My guess is that if you refactor your code using the steps above (particularly the second one) it will be much clearer what you need to do next.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM