DVD Talk Forum

DVD Talk Forum (https://forum.dvdtalk.com/)
-   Tech Talk (https://forum.dvdtalk.com/tech-talk-10/)
-   -   Looping with Java help (https://forum.dvdtalk.com/tech-talk/412878-looping-java-help.html)

sureAV421 03-06-05 04:37 PM

Looping with Java help
 
Code:

import java.io.*;                  // For IOException, BufferedReader


public class StudentStatus
{
  public static void main(String args[]) throws IOException
  {
    String studentName;            // Place to hold input name
    int numCourses;                                  // Number of courses taken
    String courseName;                          // Course Name
    int score1;                    // First test score
    int score2;                    // Second test score
    int score3;                    // Third test score
    double GPA=0.0;                                          // GPA
    double gradeSum=0.0;
    int count;
    int fail;
    BufferedReader in;            // Input source

    // Input name and three scores
    in = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter your name:");
    studentName = in.readLine();

        System.out.print("How many courses did you take this semester?");
        numCourses = Integer.parseInt(in.readLine());

    System.out.print("Enter first score: ");
    score1 = Integer.parseInt(in.readLine());
    System.out.print("Enter second score: ");
    score2 = Integer.parseInt(in.readLine());
    System.out.print("Enter third score: ");
    score3 = Integer.parseInt(in.readLine());

    gradeSum = (score1 + score2 + score3);
    GPA = (gradeSum)/numCourses;
    // Create a student record

System.out.println ("");
System.out.println ("Name: " + studentName);
System.out.println ("#Courses: " + numCourses);
System.out.println ("GPA: " + GPA);
 // Output final message
  }
}

what i'm having trouble with is after i input the number of courses taken, i need to ask that many time what their grade was. So if i input that i took 5 courses, i need to ask 5 times what the grade they received. Obviously the "score1,2,3" would need to be changed but i put it there so you could see what i was going for.

sureAV421 03-06-05 06:08 PM

ok, i've got a loop that somewhat works. the only problem i'm having is how do i add all of the input integers while running the loop? meaning if i enter 5, 4, 3 i need that to write 12 to "score".

Code:

{
        System.out.print("How many courses did you take this semester?");
        numCourses = Integer.parseInt(in.readLine());
        {
                int i=0;
                while (i < numCourses)
                {
                System.out.println("Enter score: ");
                i++;
                score = Integer.parseInt(in.readLine());
                score++;
                                }
        }
}


nodoubt 03-06-05 06:31 PM

that loop works but personally i prefer for loops.

for ur other question, take out ur score++ and use
score += Integer.parseInt(in.readLine());

dont forget to initialize score to 0

Einsatz 03-06-05 06:46 PM

As was stated, a for loop will probably be safer. While loops are better left for other types of iterations such as "while [read from file] != eof" type loops. If you're comparing to a value, that's kind of the point of a For loop.
As for writing out the sum to all inputs, you seem to be misusing your score variable... If I'm reading the snipplet properly. When you do score++ it jsut adds one to whatever the user typed in. Try the following:

System.out.print("How many courses did you take this semester?");
//you may have these two ints defined elsewhere already...
int numCourses = Integer.parseInt(in.readLine());
int score;
for(int i=0;i<numCourses;i++) {
System.out.println("Enter score: ");
score += Integer.parseInt(in.readLine());
}
System.out.println("Your total score is " + score + ".");

Everytime it loops, it'll add the input to the running total kept in the score variable.

Einsatz 03-06-05 06:48 PM

for some reason it's not letting me edit my post, but as nodoubt mentioned, the line "int score;" should read "int score=0;" ;)

sureAV421 03-06-05 07:09 PM

one more thing. When i'm inputting the score received I want to be able to hit "A" and have it registered as being a "4", a "B" would be a "3", a "C" would be a "2"...etc. is this possible?

Einsatz 03-06-05 08:00 PM

You can have a case statement to check an incoming Char and map it to an int. Such as:

switch (theChar) {
case 'A':
score+=3;
break;
case 'B':
score+=2;
break;
case 'C':
score+=1;
break
}

Of course that's just to give you an idea. You'll need to capture lower case and have some check to see if the user entered a number or a char, and capture invalid inputs. It may be easier to do a set of if statements to make it a little more robust.

nodoubt 03-06-05 08:26 PM

probably shouldn't have given as much code as we did.


All times are GMT -5. The time now is 03:48 PM.


Copyright 2021 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.