Go Back  DVD Talk Forum > General Discussions > Tech Talk
Reload this Page >

Looping with Java help

Tech Talk Discuss PC Hardware, Software, Internet and Other Technology

Looping with Java help

Old 03-06-05, 05:37 PM
  #1  
Senior Member
Thread Starter
 
Join Date: Feb 2003
Posts: 744
Likes: 0
Received 0 Likes on 0 Posts
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.
Old 03-06-05, 07:08 PM
  #2  
Senior Member
Thread Starter
 
Join Date: Feb 2003
Posts: 744
Likes: 0
Received 0 Likes on 0 Posts
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++;
				}
	}
}
Old 03-06-05, 07:31 PM
  #3  
Senior Member
 
Join Date: Sep 2002
Posts: 916
Likes: 0
Received 0 Likes on 0 Posts
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
Old 03-06-05, 07:46 PM
  #4  
Senior Member
 
Join Date: Jul 2001
Location: DC Metro Area
Posts: 379
Likes: 0
Received 0 Likes on 0 Posts
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.
Old 03-06-05, 07:48 PM
  #5  
Senior Member
 
Join Date: Jul 2001
Location: DC Metro Area
Posts: 379
Likes: 0
Received 0 Likes on 0 Posts
for some reason it's not letting me edit my post, but as nodoubt mentioned, the line "int score;" should read "int score=0;"
Old 03-06-05, 08:09 PM
  #6  
Senior Member
Thread Starter
 
Join Date: Feb 2003
Posts: 744
Likes: 0
Received 0 Likes on 0 Posts
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?
Old 03-06-05, 09:00 PM
  #7  
Senior Member
 
Join Date: Jul 2001
Location: DC Metro Area
Posts: 379
Likes: 0
Received 0 Likes on 0 Posts
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.
Old 03-06-05, 09:26 PM
  #8  
Senior Member
 
Join Date: Sep 2002
Posts: 916
Likes: 0
Received 0 Likes on 0 Posts
probably shouldn't have given as much code as we did.

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Thread Tools
Search this Thread

Archive - Advertising - Cookie Policy - Privacy Statement - Terms of Service - Do Not Sell My Personal Information

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