Having trouble with loops

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 8
Reputation: PRob99 is an unknown quantity at this point 
Solved Threads: 0
PRob99 PRob99 is offline Offline
Newbie Poster

Having trouble with loops

 
0
  #1
Sep 22nd, 2008
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 );
}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Having trouble with loops

 
2
  #2
Sep 22nd, 2008
Remive the semi-colon by your while-statement and you should be in business =)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 8
Reputation: PRob99 is an unknown quantity at this point 
Solved Threads: 0
PRob99 PRob99 is offline Offline
Newbie Poster

Re: Having trouble with loops

 
0
  #3
Sep 23rd, 2008
Originally Posted by Alex Edwards View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Having trouble with loops

 
0
  #4
Sep 23rd, 2008
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Having trouble with loops

 
0
  #5
Sep 23rd, 2008
Originally Posted by PRob99 View Post
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
  1. while (grade >= 0)
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 27
Reputation: kingvjack is an unknown quantity at this point 
Solved Threads: 0
kingvjack kingvjack is offline Offline
Light Poster

Re: Having trouble with loops

 
0
  #6
Sep 23rd, 2008
Originally Posted by PRob99 View 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 );
}
}
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...
Steve
FA LSI Logic
Wichita KS
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 596 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC