0

Making a grade book that will work for one student, it take student's name as input. Then they are asked to input grades into three categories: Homework, Quizzes, and Tests. The grades in each category will each be averaged and at the end, the formula "Final Average = 0.25*HomeworkAvg + 0.25QuizAvg + 0.50TestAvg = 65

Everything is working properly BUT -1 is not ending the process! It's frustrating!

import java.util.Scanner; 

public class Assignment3

{
public static void main( String[] args ) 
{ 
    Scanner input = new Scanner( System.in );

    int homeworkGrades;
    int quizGrades;
    int testGrades;
    int choice;
    int total;
    double average;
    String name;

    total = 0;
    homeworkGrades = 0;

        System.out.println( "Enter 1 or 2: \n 1 - Average grades \n 2 - Quit" ); 
        choice = input.nextInt();

        if ( choice == 1 ) {
            System.out.println( "Enter the students name" );
            name = input.next();



            System.out.println( " What would you like to do? \n 1 - Homework grades \n 2 - Quiz grades \n 3 - Test grades " );
            choice = input.nextInt();

            {

                while ( choice == 1 ) {
                    System.out.println( "Enter a homework grade. Press -1 when finished" );
                    homeworkGrades += input.nextInt();

                    if ( homeworkGrades != -1 ) //it's something going on right here :s

                    if ( homeworkGrades == -1 )
                        System.out.println("Total for homework grades is " + homeworkGrades );      
                }
            }
        }

    else if ( choice == 2 ) {
        System.out.println( "Exiting program" );
    }

    else {
        System.out.println( "Invalid response, exiting program." ); 
    }       
}

}

2
  • Check the code again, it has a defect. Commented Feb 12, 2014 at 17:55
  • Imagine I enter this 90, 86, 50, -1. homeworkGrades is now 225, not -1 like you think. Commented Feb 12, 2014 at 18:01

3 Answers 3

1

Your code has if ( homeworkGrades != -1 ) followed immediately by if ( homeworkGrades == -1 ) - this is probably not going to do what you want it to do.

I recommend always putting braces around your if statements and using proper code indenting to avoid bugs like this - it'll become a lot easier to detect them on a readthrough.

Sign up to request clarification or add additional context in comments.

Comments

0

( homeworkGrades == -1 ) will always be false. That is why your code is not working.

Change your code to:

while ( choice == 1 ) {
    int val = -2;
    System.out.println( "Enter a homework grade. Press -1 when finished" );
    while (val != -1) {
        val = input.nextInt();
        homeworkGrades += val;
    }
    System.out.println("Total for homework grades is " + homeworkGrades );
}

6 Comments

How can I make it to where if they enter -1, then it will end, show them the total and allow them to move on
You need 2 variables, one for the total amount of grades and another for your loop condition. Right now they are the same.
This worked, thanks! All you did was create an integer inside of the while?
Yes. As the user types values, as long as the value is not -1, you need to add it to the homework grades. The notion of as long as translates to updating the variable inside the while loop. The second important point is mentioned by @Takendarkk: you needed two variables.
Alright, thanks! Also can I ask, why did you set the int val = -2; ?
|
0

The code contain loop condition at :

while ( choice == 1 ) -> IF choice value is 1 then thr will be infinite loop.

First update this and add bracket in

if ( homeworkGrades != -1 )
{
 // Add ..
}
if ( homeworkGrades == -1 )
{
   System.out.println("Total for homework grades is " + homeworkGrades );   
}   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.