Results 1 to 2 of 2
  1. #1
    donal-ctn is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default 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


    Any ideas?

    Thanks lads! :smile:

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    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

Similar Threads

  1. Loop problem (while)
    By roud9 in forum New To Java
    Replies: 10
    Last Post: 09-30-2010, 01:02 AM
  2. Problem with the while loop
    By molokomesto in forum New To Java
    Replies: 4
    Last Post: 02-14-2010, 06:01 PM
  3. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  4. if else loop problem
    By Ms.Ranjan in forum New To Java
    Replies: 12
    Last Post: 04-25-2009, 10:30 AM
  5. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •