Help with For Loop

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

Join Date: Feb 2009
Posts: 6
Reputation: guatemalagirl is an unknown quantity at this point 
Solved Threads: 0
guatemalagirl guatemalagirl is offline Offline
Newbie Poster

Help with For Loop

 
0
  #1
Feb 16th, 2009
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Help with For Loop

 
0
  #2
Feb 16th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 6
Reputation: guatemalagirl is an unknown quantity at this point 
Solved Threads: 0
guatemalagirl guatemalagirl is offline Offline
Newbie Poster

Re: Help with For Loop

 
0
  #3
Feb 16th, 2009
what should i initialize it as?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 25
Reputation: BrianK123 is an unknown quantity at this point 
Solved Threads: 1
BrianK123's Avatar
BrianK123 BrianK123 is offline Offline
Light Poster

Re: Help with For Loop

 
0
  #4
Feb 16th, 2009
Make another counter int.
For example:
  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Help with For Loop

 
0
  #5
Feb 16th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 6
Reputation: guatemalagirl is an unknown quantity at this point 
Solved Threads: 0
guatemalagirl guatemalagirl is offline Offline
Newbie Poster

Re: Help with For Loop

 
0
  #6
Feb 16th, 2009
didnt work
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Help with For Loop

 
0
  #7
Feb 16th, 2009
which is why i said it won't work...

think logically through your code and it should be pretty simple what is wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: danielernesto is an unknown quantity at this point 
Solved Threads: 1
danielernesto danielernesto is offline Offline
Newbie Poster

Re: Help with For Loop

 
0
  #8
Feb 16th, 2009
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".
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 8
Reputation: johannady has a little shameless behaviour in the past 
Solved Threads: 0
johannady johannady is offline Offline
Newbie Poster

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

 
0
  #9
Feb 16th, 2009
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 />");
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 8
Reputation: johannady has a little shameless behaviour in the past 
Solved Threads: 0
johannady johannady is offline Offline
Newbie Poster

help me convert these for loops intyo while loops ASAP please

 
0
  #10
Feb 16th, 2009
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 />");
}
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: 561 | Replies: 10
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC