Results 1 to 4 of 4
  1. #1
    Austinw is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default I'm having trouble getting this for loop to work correctly

    So I'm trying to create a for loop that will allow a user to to get their grade average in any one class based on assignments done and their total scores on them. But when I don't know how to get it to add all of the individual assignments together and create a complete sum. instead it just takes the last assignment then adds 1 too the score. If someone could just explain to me or direct me the correct direction as to how I can get the sums of each individual assignments too add up with each other into a sum it would be much appreciated!
    Java Code:
    //Your Name
    //Assignment
    //Date
    
    import java.util.Scanner; //needed for input
    
    public class TestB { //this class name must match your workspace name
    
    
    
    	public static void main(String[] args) {
    		
    
    		Scanner input = new Scanner(System.in); //needed for input
    		
    		String anotherCourse = "yes"; //variable to control running program again
    		{
    
    			String StudentName = " " ;
    			String CourseName = " ";
    			int Assignments = 1;
    			//Declare variables
    			//-------------------------------------------------	
    			System.out.println("Class Grade Calculator");	//Display header 
    	
    		System.out.println("Enter Student Name here: ");	//Ask for student name and course name
    		StudentName = input.next();
    		
    		System.out.println("Enter Course Name: ");
    		CourseName = input.next();
            
    		System.out.println("Enter How many assignments there are: " );
    		Assignments = input.nextInt();//Ask how many assignments were taken
    	 
    		//-------------------------------------------------------		
    			int pointsPossible = 0;
    			int pointsEarned = 0 ;
    			double currentPercent = 0;
                String letterGrade = " ";
                double totalpossible = 0 ;
                double totalearned = 0 ;
    			int currentgrade = 0 ;
                for(int i=0; i < Assignments; i++)
                	
              	{
                	
                	
    
                	
                	
    				System.out.println("How many points was the assignment worth? ");
    				pointsPossible = input.nextInt();
    				
    				totalpossible = pointsPossible + i;
    				
    				System.out.println("How many points did you score? ");
    				pointsEarned = input.nextInt();
    			
    				totalearned = pointsEarned + i;
    				
    				if (currentPercent <= 100 && currentPercent >= 93)
    					letterGrade = "A";
    				else if (currentPercent < 93 && currentPercent >= 90)
    					letterGrade = "A-";
    				if (currentPercent <= 89 && currentPercent >= 83)
    					letterGrade = "B";
    				else if (currentPercent < 89 && currentPercent >= 80)
    					letterGrade = "B-";
    				if (currentPercent <= 100 && currentPercent >= 93)
    					letterGrade = "C";
    				else if (currentPercent < 93 && currentPercent >= 90)
    					letterGrade = "C-";
    				if (currentPercent <= 89 && currentPercent >= 83)
    					letterGrade = "D";
    				else if (currentPercent < 89 && currentPercent >= 80)
    					letterGrade = "D-";
    				else if (currentPercent<=59)
    				    letterGrade="F";
    			
    			
    			
    				totalearned = pointsEarned + i;
    				totalpossible = pointsPossible + i;
    				currentPercent = pointsEarned/pointsPossible;
    				currentgrade = i*100;
    				
    			
    			}
    			
    			
    			currentPercent = totalearned/totalpossible;
    			
    		    System.out.println("Your Class Statistics!");
    		    System.out.println("Totl possible");
    		    System.out.println(totalpossible);
    		    System.out.println("Total Earned");
    		    System.out.println(totalearned);
    			System.out.printf("Total percent to date...%.2f%%\n", currentPercent);
    			System.out.printf("Grade in class is: " + currentgrade );
    			letterGrade = input.next();
    			System.out.println(""); //space
    			
    			//-------------------------------------------------------
    			}	
    			{
    		
    		System.out.print("Enter yes if there is another class you want to calculate: ");
    		anotherCourse = input.next();
    		input.nextLine(); // causes skipping issue to fix
    		System.out.print("\n\n\n");
    	} while (anotherCourse.equalsIgnoreCase("yes"));
    		
    		
    		
    		
    	} // end of main
    
    }// end of class

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: I'm having trouble getting this for loop to work correctly

    Java Code:
    public static void main(String[] args) {
     
        Scanner input = new Scanner(System.in); //needed for input
             
        String anotherCourse = "yes"; //variable to control running program again
        {
    That { at the end of this fragment from the opening part of main() looks pretty weird. You don't normally see opening brackets sitting like that without being part of some larger structure (loop etc). What is it supposed to be doing?

  3. #3
    Austinw is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: I'm having trouble getting this for loop to work correctly

    I think it's just apart of the main body, I don't know really know haha, whenever I started the new Java Project it was already there I beleive

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: I'm having trouble getting this for loop to work correctly

    Is it one of these "fill in the gaps" assignments? If so, post the whole thing. No-one here does people's homework for them (and those who do get zapped by the moderati).

    I'm wondering if that { was really part of a larger structure. I'm talking about the { that appears on line 17, the first one is fine and probably was inserted by your IDE.

Similar Threads

  1. Replies: 18
    Last Post: 08-09-2011, 09:08 AM
  2. For loop trouble
    By 2wyked in forum New To Java
    Replies: 7
    Last Post: 03-27-2011, 06:50 PM
  3. Cant make my object streams work correctly
    By Ruuhkis in forum Networking
    Replies: 0
    Last Post: 03-14-2011, 07:54 PM
  4. Trouble with for loop
    By subiedude101 in forum New To Java
    Replies: 4
    Last Post: 02-13-2011, 10:27 PM
  5. trouble with loop
    By aamster in forum New To Java
    Replies: 10
    Last Post: 10-16-2009, 10:18 PM

Tags for this Thread

Posting Permissions

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