943,582 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 775
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 16th, 2009
0

Help with For Loop

Expand Post »
the purpose of my program is to ask the user for their numerical grades (depending on how many total grades they have) and then they are given their letter grade. but i cant get the for loop to loop the question according to their number of total grades

JAVA Syntax (Toggle Plain Text)
  1. import TerminalIO.*;
  2. public class LetterGradeWLT {
  3. public static void main(String [] args){
  4.  
  5.  
  6.  
  7. KeyboardReader reader = new KeyboardReader();
  8.  
  9. int grades;
  10. int grade;
  11. String lettergrade;
  12.  
  13. grades = reader.readInt("Enter total number of grades: ");
  14.  
  15.  
  16.  
  17. for ( grades = 1; grades >= 1; grades++) {
  18.  
  19. grade = reader.readInt("Enter your numeric grade: ");
  20.  
  21.  
  22. if (grade >= 96)
  23. System.out.println(" Your letter grade is A+.");
  24. if ( 92 <= grade && grade <= 95)
  25. System.out.println(" Your letter grade is A.");
  26. if ( 90 <= grade && grade <= 91)
  27. System.out.println(" Your letter grade is A-.");
  28. if ( 86 <= grade && grade <= 89)
  29. System.out.println(" Your letter grade is B+.");
  30. if ( 82 <= grade && grade <= 85)
  31. System.out.println(" Your letter grade is B.");
  32. if ( 80 <= grade && grade <= 81)
  33. System.out.println(" Your letter grade is B-.");
  34. if ( 76 <= grade && grade <= 79)
  35. System.out.println(" Your letter grade is C+.");
  36. if ( 72 <= grade && grade <= 75)
  37. System.out.println(" Your letter grade is C.");
  38. if ( 70 <= grade && grade <= 71)
  39. System.out.println(" Your letter grade is C-.");
  40. if ( 66 <= grade && grade <= 69)
  41. System.out.println(" Your letter grade is D+.");
  42. if ( 62 <= grade && grade <= 65)
  43. System.out.println(" Your letter grade is D.");
  44. if ( 60 <= grade && grade <= 61)
  45. System.out.println(" Your letter grade is D-.");
  46. if ( -1 < grade && grade <= 59)
  47. System.out.println(" Your letter grade is F.");
  48. }
  49. }
  50. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
guatemalagirl is offline Offline
6 posts
since Feb 2009
Feb 16th, 2009
0

Re: Help with For Loop

for ( grades = 1; grades >= 1; grades++)

so you initialise grades to 1. (why? considering you ask the user for grades... you are overwriting what you want to know)

you want to loop if grades is greater or equal to 1.

each loop you want to increment grades by 1.

does this logic add up?
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 16th, 2009
0

Re: Help with For Loop

what should i initialize it as?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
guatemalagirl is offline Offline
6 posts
since Feb 2009
Feb 16th, 2009
0

Re: Help with For Loop

Make another counter int.
For example:
Java Syntax (Toggle Plain Text)
  1. int grades;
  2. int grade;
  3. int counter;
  4. String lettergrade;
  5.  
  6. grades = reader.readInt("Enter total number of grades: ");
  7.  
  8.  
  9.  
  10. for ( counter = 1; counter >= grades; counter++) {
  11. etc.
that should do it I believe
Reputation Points: 10
Solved Threads: 1
Light Poster
BrianK123 is offline Offline
25 posts
since Feb 2009
Feb 16th, 2009
0

Re: Help with For Loop

that won't work, but the idea is there. you don't even need to introduce a separate counter, but you should really try work this out yourself.

note: you don't need to initialise inside a for loop if you don't want.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 16th, 2009
0

Re: Help with For Loop

didnt work
Reputation Points: 10
Solved Threads: 0
Newbie Poster
guatemalagirl is offline Offline
6 posts
since Feb 2009
Feb 16th, 2009
0

Re: Help with For Loop

which is why i said it won't work...

think logically through your code and it should be pretty simple what is wrong.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 16th, 2009
0

Re: Help with For Loop

You should try an normal loop, a nice loop with my friend the varible "i", then continue , until my varible "i" is less than grades, in the mean time the loop will be "looping".
Reputation Points: 9
Solved Threads: 2
Newbie Poster
danielernesto is offline Offline
16 posts
since Nov 2007
Feb 16th, 2009
0

can you convert these for loops to while loops for me... please...

number = 10;
sum = 0;
for (var i=1; i<= number; i++) {
sum = sum + number;
}
document.write("sum = " + sum);


number = 10;
for (var i=number; i > 0; i--) {
if ( i%2 == 1)
document.write(i);
}


product = 1;
number = 5;
for (var i=1; i < (number+1); i++) {
product = product * number;
}
document.write(product);


for ( var i=1; i<= 5; i++) {
for (var j=1; j<=3; j++) {
document.write("*");
}
document.write("<br />");
}
Reputation Points: 5
Solved Threads: 0
Newbie Poster
johannady is offline Offline
8 posts
since Feb 2009
Feb 16th, 2009
0

help me convert these for loops intyo while loops ASAP please

number = 10;
sum = 0;
for (var i=1; i<= number; i++) {
sum = sum + number;
}
document.write("sum = " + sum);


number = 10;
for (var i=number; i > 0; i--) {
if ( i%2 == 1)
document.write(i);
}


product = 1;
number = 5;
for (var i=1; i < (number+1); i++) {
product = product * number;
}
document.write(product);


for ( var i=1; i<= 5; i++) {
for (var j=1; j<=3; j++) {
document.write("*");
}
document.write("<br />");
}
Reputation Points: 5
Solved Threads: 0
Newbie Poster
johannady is offline Offline
8 posts
since Feb 2009

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: identifier experced error help!
Next Thread in Java Forum Timeline: How to ouput the tokenized string





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


Follow us on Twitter


© 2011 DaniWeb® LLC