Search code examples
javascriptmethodsgetter-setter

The program does not show the calculations result... only zeroes


private static String name;   // attributes 
private static int numberQuize;
private static int totalScores;

public static String getName(){
    return name;
}
	
public void setName(String name)
{
    this.name = name;
}

public int getNumberQuize()
{
    return numberQuize;
}

public  void setNumberQuize(int numberQuize)
{
    this.numberQuize = numberQuize;
}

public static int getTotalScores()
{
    return totalScores;
}

public void setTotalScores(int totalScores)
{
    this.totalScores = totalScores;
}
	
public int totalScores(){      //Methods (adding quiz, totalScore, AverageScore)
    return totalScores;
}
	
public void addQuiz (int score){
    for	(int quiz = 0; quiz >=0 ; quiz++){
        totalScores = totalScores + score;
    }
		
}
	
public double AverageScore (){    
    return (double) totalScores/numberQuize;
}

I wrote this code and it should display student name and total score nd the average score. However, the code runs but no answers it only show me zeroes.. I tried everything I know but still nothing is working..


Solution

  • Your code has an infinite loop here:

    public void addQuiz (int score){
        for (int quiz = 0; quiz >=0 ; quiz++){
            totalScores = totalScores + score;
        }
    }
    

    quiz will always be >= 0.

    I think your addQuiz() method should be:

    public void addQuiz(int score){
        totalScores += score;
        numberQuize += 1;
    }