-
for loop problem
Hi lads new here and right in with my first question!
Im doing an program that according to my introduction
"This java application is used to take in two classes, how many students are in the class and the grades they acheived from that class. It then evaluates the grades by if the person passed, failed and the average grade.
The program will tell you the amount of pass and fails per class. The higher/equalled average grade from each class will be displayed at the end of the program."
A while loop must be used for the first class and a for loop for the second. The while is fine but the for loop wont do what i want. When i enter the number of students e.g 5 it will only ask for 3 grades?. heres what i have so far
/**
* @(#)project2.java
* Enter grades for 2 classes. Use a while loop for one class and a for loop for the other class.
* The user should be asked before entering each class, how many students are in the class and
* the loop will execute that many times to take in the grades.
* Count the number of fails and passes in the class. Tell the user the highest and lowest and average grade from each class.
* Check for non- digits being entered when taking the number of students in the classes.
* Tell the user the name of the class got the better average or if both classes got the same average.
* While loops and for loops are to be used in the project.
* @author
* @version 1.00 2010/11/17
*/
import java.util.Scanner; //Needed for entering data
public class project2 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in); //Allows us to enter data
int i=1,grade=0,high1=0,low1=100,high2=0,low2=100,pass 1=0,fail1=0,pass2=0,fail2=0,av
erage1=0,average2=0,numgrades;
int total1=0, total2=0;
String class1,class2,students;
//prompt user for class name
System.out.print("Enter class name : ");
class1 = input.next();
//prompt user for number of student in class 1
System.out.print("How many students are in the class? : ");
students = input.next();
//check for non digits
while (!students.matches("\\d+")){
//prompt user
System.out.println("Error, numbers only");
System.out.print("Enter number of Students : ");
students = input.next();
}// end while
//change students to an int
numgrades = Integer.parseInt(students);
//while loop to take in grades
while (i<=numgrades){
//prompt user
System.out.print("Enter grade : ");
//take in grade
grade =input.nextInt();
//add grade to total
total1 += grade;
//Check for highest and lowest grades
if(grade>high1){
high1 = grade;
}//end if
if(low1>grade){
low1 = grade;
}//end if
if(grade>50){
pass1++;
}//end if
if(grade<49){
fail1++;
}//end if
i++;
}//end while
average1 = total1/numgrades;
System.out.println("The highest grade is " + high1);
System.out.println("The lowest grade is " + low1);
System.out.println(pass1 + " of the students passed");
System.out.println(fail1 + " of the students failed");
System.out.println("The average of the grades is " + average1);
//Class2____________________________________________ ______________________
System.out.println("");
System.out.println("============================== =");
System.out.println("");
System.out.println("Class 2");
//Next Class
//prompt user for class name
System.out.print("Enter class name : ");
class2 = input.next();
//prompt user for number of student in class 2
System.out.print("How many students are in the class? : ");
students = input.next();
//check for non digits
while (!students.matches("\\d+")){
//prompt user
System.out.println("Error, numbers only");
System.out.print("Enter number of Students : ");
students = input.next();
}// end while
//change students to an int
numgrades = Integer.parseInt(students);
//for loop to take in grades
for (i=1; i<=numgrades;i++){
System.out.print("Enter grade : ");
grade = input.nextInt();
//Add grade to total
total2 += grade;
//Check for highest and lowest grades
if(grade>high2){
high2 = grade;
}//end if
if(low2>grade){
low2 = grade;
}//end if
if(grade>50){
pass2++;
}//end if
else{
fail2++;
}//end else
i++;
}//end for
average2 = total2/numgrades;
System.out.println("The highest grade is " + high2);
System.out.println("The lowest grade is " + low2);
System.out.println(pass2 + " of the students passed");
System.out.println(fail2 + " of the students failed");
System.out.println("The average of the grades is " + average2);
}//end main method
}//end class
heres it in DOS
http://img535.imageshack.us/img535/5251/75237422.jpg
Any ideas?
Thanks lads! :smile:
-
You don't need to put
i++
at the end of your [for] loop.
in this way you have forsed your variable [i] to increase twice in the loop.
[for] loops already have their [i++] in their first line:
example: for(int i = 1; i <= somenumber; i++)
br