954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with For Loop

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

import TerminalIO.*;
public class LetterGradeWLT {
  public static void main(String [] args){
 

  
  KeyboardReader reader = new KeyboardReader();
  
  int grades;
  int grade;
  String lettergrade;
    
  grades = reader.readInt("Enter total number of grades: ");
    

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

     grade = reader.readInt("Enter your numeric grade: ");

    
    if (grade >= 96)
      System.out.println(" Your letter grade is A+.");
    if ( 92 <= grade && grade <= 95)
      System.out.println(" Your letter grade is A.");
    if ( 90 <= grade && grade <= 91)
      System.out.println(" Your letter grade is A-.");
    if ( 86 <= grade && grade <= 89)
      System.out.println(" Your letter grade is B+.");
    if ( 82 <= grade && grade <= 85)
      System.out.println(" Your letter grade is B.");
    if ( 80 <= grade && grade <= 81)
      System.out.println(" Your letter grade is B-.");
    if ( 76 <= grade && grade <= 79)
      System.out.println(" Your letter grade is C+.");
    if ( 72 <= grade && grade <= 75)
      System.out.println(" Your letter grade is C.");
    if ( 70 <= grade && grade <= 71)
      System.out.println(" Your letter grade is C-.");
    if ( 66 <= grade && grade <= 69)
      System.out.println(" Your letter grade is D+.");
    if ( 62 <= grade && grade <= 65)
      System.out.println(" Your letter grade is D.");
    if ( 60 <= grade && grade <= 61)
      System.out.println(" Your letter grade is D-.");
    if ( -1 < grade && grade <= 59)
      System.out.println(" Your letter grade is F.");
  }
}
}
guatemalagirl
Newbie Poster
6 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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?

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
 

what should i initialize it as?

guatemalagirl
Newbie Poster
6 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Make another counter int.
For example:

int grades;
  int grade;
int counter;
  String lettergrade;
 
  grades = reader.readInt("Enter total number of grades: ");
 
 
 
   for ( counter = 1; counter >= grades; counter++) {
etc.

that should do it I believe

BrianK123
Light Poster
25 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

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.

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
 

didnt work :(

guatemalagirl
Newbie Poster
6 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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

think logically through your code and it should be pretty simple what is wrong.

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
 

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".

danielernesto
Newbie Poster
16 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
 

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("
");
}

Johannady2
Light Poster
40 posts since Feb 2009
Reputation Points: 5
Solved Threads: 0
 

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("
");
}

Johannady2
Light Poster
40 posts since Feb 2009
Reputation Points: 5
Solved Threads: 0
 

I suppose you need to ask for the number of grades and then continue looping in the for loop for those many times.
Now, if you understand the for loop construct,
initialise a counter to 1 and continue it to the number of grades, incrementing it once every time.
Think on this and let us know the result.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You