943,866 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 751
  • Java RSS
Sep 22nd, 2008
0

Having trouble with loops

Expand Post »
Ok, I am having trouble getting this loop to continue. It just asks once and that's it. Here is my code:

// ----------------------------------------------------------
// ExamScores.java (Application)
//
// Author:
// Entered by: -- --
// Classes: ExamScores (list all the class files related to this problem)
// Date: September 22, 2008
// Description: Creating a loop to receive input of grades and outputing the subsequent letter grade
// Bugs: No known bugs.
// ----------------------------------------------------------
import java.util.*;

public class ExamScores
{

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

int gradeCounter;
int A=0;
int B=0;
int C=0;
int D=0;
int F=0;
double grade =0;

gradeCounter = 0;

System.out.print("Enter an exam score (to quit enter a negative score): ");
grade = input.nextDouble();

while (grade !=-1);
{
System.out.println("Enter an exam score (to quit enter a negative score): ");
grade = input.nextDouble();

if(grade >=90)
A++;
else
if(grade >=80)
B++;
else
if(grade >=70)
C++;
else
if(grade >=60)
D++;
else
F++;

gradeCounter = gradeCounter + 1;

grade++;
}// end of while loop

System.out.println("A: " + A +"\nB: " + B + "\nC: " + C + "\nD: " + D + "\nF: " + F );
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PRob99 is offline Offline
8 posts
since Sep 2008
Sep 22nd, 2008
2

Re: Having trouble with loops

Remive the semi-colon by your while-statement and you should be in business =)
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Sep 23rd, 2008
0

Re: Having trouble with loops

Remive the semi-colon by your while-statement and you should be in business =)
I removed the semicolon and now it will keep prompting me to enter a grade. The problem now is, when I enter a -1 it doesn't stop the infinite loop.

Also, I want to be able to enter a negative number (not just a -1) to stop the loop. Would it be possible to do this statement?: while (grade !<=-1)
Thanks in advance!
Last edited by PRob99; Sep 23rd, 2008 at 8:28 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PRob99 is offline Offline
8 posts
since Sep 2008
Sep 23rd, 2008
0

Re: Having trouble with loops

Thats cause you are doing a grade++; near the end of your loop. Try entering -2 and it should stop.
But I do not see any point in grade++; being there since you are anyways going to overwrite it with grade = input.nextDouble(); on the next iteration.

And please use code tags. Look here and here for more information on them. You should read the rules before posting.
Last edited by stephen84s; Sep 23rd, 2008 at 10:39 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Sep 23rd, 2008
0

Re: Having trouble with loops

Click to Expand / Collapse  Quote originally posted by PRob99 ...
Also, I want to be able to enter a negative number (not just a -1) to stop the loop. Would it be possible to do this statement?: while (grade !<=-1)
Thanks in advance!
try
java Syntax (Toggle Plain Text)
  1. while (grade >= 0)
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Sep 23rd, 2008
0

Re: Having trouble with loops

Click to Expand / Collapse  Quote originally posted by PRob99 ...
Ok, I am having trouble getting this loop to continue. It just asks once and that's it. Here is my code:

// ----------------------------------------------------------
// ExamScores.java (Application)
//
// Author:
// Entered by: -- --
// Classes: ExamScores (list all the class files related to this problem)
// Date: September 22, 2008
// Description: Creating a loop to receive input of grades and outputing the subsequent letter grade
// Bugs: No known bugs.
// ----------------------------------------------------------
import java.util.*;

public class ExamScores
{

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

int gradeCounter;
int A=0;
int B=0;
int C=0;
int D=0;
int F=0;
double grade =0;

gradeCounter = 0;

System.out.print("Enter an exam score (to quit enter a negative score): ");
grade = input.nextDouble();

while (grade !=-1);
{
System.out.println("Enter an exam score (to quit enter a negative score): ");
grade = input.nextDouble();

if(grade >=90)
A++;
else
if(grade >=80)
B++;
else
if(grade >=70)
C++;
else
if(grade >=60)
D++;
else
F++;

gradeCounter = gradeCounter + 1;

grade++;
}// end of while loop

System.out.println("A: " + A +"\nB: " + B + "\nC: " + C + "\nD: " + D + "\nF: " + F );
}
}
Your program is executing correctly as written. There needs to be a way of returning to the top either through another prompt to the user or by modifying your case to return or somehow include your gradeCounter variable to actually control the loop...
Reputation Points: 13
Solved Threads: 0
Light Poster
kingvjack is offline Offline
28 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: simple coin program
Next Thread in Java Forum Timeline: Java program need help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC