6 Replies - 855 Views - Last Post: 02 November 2012 - 11:32 AM Rate Topic: -----

#1 cracked_head  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-November 12

If statements keep skipping

Posted 02 November 2012 - 09:51 AM

I have a couple of issues on a homework assignment for my Java class. For the assignment I have to write a program that creates a progress report for one class asking the user for their name, the name of the course, amount of assignments that they have submitted, amount of points each of those assignments was worth, and what they earned on the assignments which then adds up the totals for points the assignment was worth and what they earned on them and divides it by the amount of submitted assignments giving them a letter grade and a percentage for their grade as well. I'm not sure if the statements are getting skipped the second time through or not but I think that's whats happening. I've spent probably 3 or 4 hours just trying to get the "if" statements to work right. I got them to work right when I first started on the code but I had a problem with the inputs. After I fixed the input issue I ran the program and now it just ignores the if statements when asking for input the second time around. I've tried to find something about this on the internet but I can't seem to find anything. I also need help with how to add the user inputs for points possible and points earned.
	public static void main(String[] args) {
	Scanner input = new Scanner (System.in);	
	String anotherCourse = "yes"; //variable to control running program again
	do {

		//Declare variables
		String studentName = " ";
		int submittedAssignments = 0;
		double pointsEarned = 0;
		double pointsPossible = 0;
		double currentPercent = 0;
		String letterGrade = " ";
		String courseName = " ";
		double totalPossible = pointsPossible++;
		double totalEarned = pointsEarned++;
		
		
		//Display header 
		System.out.println("===============================");
		System.out.println("      What's Your Grade?       ");
		System.out.println("===============================");
		//Ask for student name and course name
		System.out.print("Enter student's name: ");
		studentName = input.next();
		input.nextLine();
		System.out.print("Enter course name: ");
		courseName = input.next();
		input.nextLine();
		
		//Ask how many assignments were taken
		System.out.print("Enter the number of assignments that you have submitted");
		submittedAssignments = input.nextInt();
		//and error code if is <= to 0 they take in again
		if (submittedAssignments <= 0);
		{
			System.out.println("Enter a number greater than zero for submitted assignments");
			submittedAssignments = input.nextInt();
		}
		
		for (int i = 0; i < submittedAssignments; i++)
		{
			System.out.println("Enter amount of points the assignment was worth");
			pointsPossible = input.nextDouble();
			if (pointsPossible <=0)
			{
				System.out.println("Enter a valid point value. Must be greater than zero.\nEnter amount of points the assignment was worth: ");
				pointsPossible = input.nextDouble();

			}
			
			System.out.println("Enter amount of points you scored on the assignment");
			pointsEarned = input.nextDouble();

			do			
			{
			System.out.println("Enter a valid point value. Must be greater than or equal to zero and less than points the assignment was worth. \nEnter the amount of points you scored on the assignment: ");
						pointsEarned = input.nextDouble();
			}while (pointsEarned > pointsPossible && pointsEarned < 0);
					
		//calculate the current percentage
				currentPercent = (pointsEarned/pointsPossible) * 100;
		//Write a nested if else statement to figure out letter grade based on standard grade chart
		 	if (currentPercent <= 100 && currentPercent >= 93)
				letterGrade = "A";
			else if (currentPercent < 93 && currentPercent >= 90)
				letterGrade = "A-";
			else if (currentPercent < 90 && currentPercent >= 87)
				letterGrade = "B+";
			else if (currentPercent < 87 && currentPercent >= 83)
				letterGrade = "B";
			else if (currentPercent < 83 && currentPercent >= 80)
				letterGrade = "B-";
			else if (currentPercent < 80 && currentPercent >= 77)
				letterGrade = "C+";
			else if (currentPercent < 77 && currentPercent >= 73)
				letterGrade = "C";
			else if (currentPercent < 73 && currentPercent >= 70)
				letterGrade = "C-";
			else if (currentPercent < 70 && currentPercent >= 67)
				letterGrade = "D+";
			else if (currentPercent < 67 && currentPercent >= 63)
				letterGrade = "D";
			else if (currentPercent < 63 && currentPercent >= 60)
				letterGrade = "D-";
			else if (currentPercent < 60 && currentPercent >= 0)
				letterGrade = "F";
				
				
		//Display final output that is required and format nicely
		 System.out.println("\tProgress Report for " + studentName);
		 System.out.println("\tCourse Name is " + courseName);
		 System.out.println("===================================================");
		 System.out.println("Number of assignments submitted..." + submittedAssignments);
		 System.out.println("Total points possible............." + totalPossible);
		 System.out.println("Total points earned..............." + totalEarned);
		 System.out.printf("Total percent to date.............%.2f%%\n", currentPercent);
		 System.out.println("Letter grade to date.............." + letterGrade);
		 System.out.println("===================================================");
		
		

		//This code ends the do while to run again
		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 class
}//end of main
	


Is This A Good Question/Topic? 0
  • +

Replies To: If statements keep skipping

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: If statements keep skipping

Posted 02 November 2012 - 10:13 AM

With which if() do you have a problem

and you are using a lot of && for nothing here
            if (currentPercent <= 100 && currentPercent >= 93)  
                 letterGrade = "A";  
             else if (currentPercent < 93 && currentPercent >= 90)  
                 letterGrade = "A-";  
             else if (currentPercent < 90 && currentPercent >= 87)  
...


this would do
            if (currentPercent >= 93)  
                 letterGrade = "A";  
             else if (currentPercent >= 90)  
                 letterGrade = "A-";  
             else if (currentPercent >= 87)  
...


Was This Post Helpful? 1
  • +
  • -

#3 cracked_head  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-November 12

Re: If statements keep skipping

Posted 02 November 2012 - 10:33 AM

It's just the statements that ask for the points earned. It displays it again after you put the wrong number in but then it skips over something and it just runs the program the rest of the way through. And thank you for the && suggestion, this is for my first programming class and I took it online so I'm pretty much on my own.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: If statements keep skipping

Posted 02 November 2012 - 10:40 AM

Quote

//Write a nested if else statement to figure out letter grade based on standard grade chart

That's not actually what you've done btw (not that it's a particularly efficient way of doing it anyway)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: If statements keep skipping

Posted 02 November 2012 - 10:42 AM

As you have already asked for pointsEraned, I would change your do/while into a while

from
           do           
             {  

             System.out.println("Enter a valid point value. Must be greater than or equal to zero and less than points the assignment was worth. \nEnter the amount of points you scored on the assignment: ");  
                         pointsEarned = input.nextDouble();  
             }while (pointsEarned > pointsPossible && pointsEarned < 0);  


to
           while (pointsEarned > pointsPossible && pointsEarned < 0)        
             {  

             System.out.println("Enter a valid point value. Must be greater than or equal to zero and less than points the assignment was worth. \nEnter the amount of points you scored on the assignment: ");  
                         pointsEarned = input.nextDouble();  
             }


Was This Post Helpful? 0
  • +
  • -

#6 cracked_head  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-November 12

Re: If statements keep skipping

Posted 02 November 2012 - 10:44 AM

View Postg00se, on 02 November 2012 - 10:40 AM, said:

Quote

//Write a nested if else statement to figure out letter grade based on standard grade chart

That's not actually what you've done btw (not that it's a particularly efficient way of doing it anyway)


The standard grade chart is just an A>93% and so on. I also know that it's not a very efficient way but that's what I have to do for the assignment.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: If statements keep skipping

Posted 02 November 2012 - 11:32 AM

Quote

I also know that it's not a very efficient way but that's what I have to do for the assignment.
Yes, that's right but my point is that you're not fulfilling the assignment (as the if statements are NOT nested)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1