| | |
Having trouble with loops
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 8
Reputation:
Solved Threads: 0
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 );
}
}
// ----------------------------------------------------------
// 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 );
}
}
•
•
Join Date: Sep 2008
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
Remive the semi-colon by your while-statement and you should be in business =)
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.
Thats cause you are doing a
But I do not see any point in
And please use code tags. Look here and here for more information on them. You should read the rules before posting.
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 ?"
"How to ask questions the smart way ?"
•
•
•
•
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!
java Syntax (Toggle Plain Text)
while (grade >= 0)
•
•
Join Date: Aug 2006
Posts: 27
Reputation:
Solved Threads: 0
•
•
•
•
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 );
}
}
Steve
FA LSI Logic
Wichita KS
FA LSI Logic
Wichita KS
![]() |
Similar Threads
- Program Help Using For Nested Loops (C++)
- Help with Nested Loops, C (C)
- Having trouble calculating the median from a data file (C++)
- A program with Menus and Loops...HELP!! (Java)
- Having trouble displaying an Arrays. Please help. (C)
- Need help with C++ reguarding loops and rfind (C++)
- Help w/Structs and 'For' loops (C++)
Other Threads in the Java Forum
- Previous Thread: simple coin program
- Next Thread: Java program need help!
Views: 597 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api apple applet application arguments array arrays automation binary blackberry block bluetooth chat class classes client code component database detection developmenthelp draw eclipse encode error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie number object online oracle os page print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing template test textfields threads time title transfer tree tutorial-sample update windows working






