Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Cannot get the correct output

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Cannot get the correct output

    Hello guys, I need your help. I want to know why the average score and grade are wrong? Please give me a hint how to solve this problem. Thanks in advance.

    Output
    Enter your quiz 1 score: 4
    Enter your quiz 2 score: 4
    Enter your quiz 3 score: 4
    The average score is: 0.0
    The quiz grade is: A

    import java.util.Scanner;
    public class Questiontwo 
    {
        public static void main(String [] args)
        {
        	Scanner input = new Scanner(System.in);
        	float score = 0;
        	float averagescore = 0;
        	String grade = "A";
     
        	for (int i = 1; i < 4; i++)
        	{
        		System.out.print("Enter your quiz " + i + " score: ");
        		score = input.nextFloat();
        	}
     
        	System.out.println("The average score is: " + averagescore);
        	System.out.println("The quiz grade is: " +grade);
        }
     
        public static float GetAverageScore(float averagescore, float score)
        {
        	averagescore = averagescore + score;
        	return averagescore;
        }
     
        public static String GetQuizGrade(float averagescore, String grade)
        {
        	if (averagescore >= 8 && averagescore <= 10)
        	{
        		grade = "A";
        		return grade;
        	}
     
        	else if (averagescore >= 5 && averagescore <= 7.99)
        	{
        		grade = "B";
        		return grade;
        	}
     
        	else if (averagescore >= 0 && averagescore <= 4.99)
        	{
        		grade = "C";
        		return grade;
        	}
     
        	else
        	{
        		grade = "F";
        		return grade;
            }   
        }
    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Cannot get the correct output

    Quote Originally Posted by akif13 View Post
    I want to know why the average score and grade are wrong?
    From your code it's evident that GetAverageScore and GetQuizGrade methods are not used. You are only doing a simple for loop, overwriting at every cycle the score variable and without doing any particular calculation. So what can you expect from this?
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. For loop not giving the correct output
    By Kattracks32 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 28th, 2013, 04:41 AM
  2. My loop compiles and seems correct, but gives wrong output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 15th, 2013, 08:35 PM
  3. Not getting the correct output
    By Ashish S in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 26th, 2012, 03:56 AM
  4. Not getting the correct output
    By KNAYERS in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 23rd, 2012, 01:59 PM
  5. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM