user2518318
user2518318

Reputation: 13

Have user enter scores or check grade average in 2 dimensional array?

Help! I feel like I am in way over myhead, I am trying to create a small program to have a user do the following

Pseudo Code

Import scanner

Define variables numStudents numExams studscore studgrade userIn keepGoing

Ask user how many students, set to numStudents

Ask user how many exams, set to numExams

Create 2 dimensional array studentResults for numstudents numExams

While keepGoing == true

Ask user to Press E to exit, S to enter exam scores for a student, or C to check a final grade Parse input as char If exit, exit program, set keepGoing to false Else if S, ask for student number, create For statement to have user enter all exam scores and store for that student number Else if C, ask for student number, display results Else showmessage, that is not a valid option

***So far I have this....

I am trying to find out how to have the user input their scores (Asks what their student number is (first array part) then input their exam scores for that student number.

And then part 2, coming back into the while loop, if they select to view final grade, program asks their student number and returns the average of the scores for that student number.

import java.util.*;
import javax.swing.JOptionPane;
public class examgrades {

    public static void main(String[] args)
    {

    //Define variables
    int numStudents=0;
    int numExams=0;
    int studScore=1;
    char studGrade='a';
    int studChoice=1;
    char userChoice;
    boolean keepGoing=true;

    //Get number of students
    System.out.println("Please enter the number of Students: ");
    Scanner reader = new Scanner(System.in);
    numStudents=reader.nextInt();

    //Get number of Exams   
    System.out.println("Thank you, now please enter the number of exams this semester: ");
    Scanner reader1 = new Scanner(System.in);
    numExams=reader1.nextInt();

    //Create array
    int [][] studentResults = new int [numStudents][numExams];

    //Create intial program loop
    while (keepGoing);

        //Ask user to Press S to enter exam scores for a student, C to check a final grade, or E to Exit
        System.out.println("Press S to enter exam scores for a student, C to check a final grade, or E to Exit");
        Scanner reader2 = new Scanner(System.in);
        String userIn=reader2.nextLine();
        //Parse into Char
        userChoice=userIn.charAt(0);

        //Check for exit
        if (userChoice=='e' || userChoice=='E') {keepGoing=false; System.out.println("Program is exiting... \n");}

        //Enter Scores if User chose S


        //Check final Grade if user chose C

Upvotes: 0

Views: 2434

Answers (1)

Óscar López
Óscar López

Reputation: 235984

Here'a a hard to catch bug in your code:

while (keepGoing);

The correct way to declare a loop is this:

while (keepGoing) {
    // body
}

That ; at the end of the loop condition has the effect of creating a loop without a body, so nothing is being repeated! the code below the loop is executing exactly once.

Upvotes: 3

Related Questions