Home > Net >  Why this "double mf = mediaFinal(nota1, nota2, nota3);" returns 0?
Why this "double mf = mediaFinal(nota1, nota2, nota3);" returns 0?

Time:01-25

package emcasa;

import java.util.Scanner;

public class Aluno {
    Scanner scanner = new Scanner(System.in);
    
    
    
    String nome;
    String matricula;
    int nota1;
    int nota2;
    int nota3;
    
    
    
    double mediaFinal (double nota1, double nota2 , double nota3){
        return (( nota1 * 2.5)   (nota2 * 2.5)   (nota3 * 2)) / 7 ;
        
    }
    
    double mf = mediaFinal(nota1, nota2, nota3);
    
    void fim () {
        if ( mf >= 7 ) {
            System.out.println("Aprovado!");
        }
        else {
            System.out.println(nome   " precisa tirar "   ( 10 - mf)   " na prova final");
            
        }
    }
    
}

Why this double mf = mediaFinal(nota1, nota2, nota3); returns 0?

But at main class returns correct?

package emcasa;
import java.util.Scanner;

public class QuestaoJoao {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Aluno a = new Aluno();
        
        a.nome = "Rafael Arruda";
        a.matricula = "2023.1REC0001";      
        a.nota1 = 5;
        a.nota2 = 6;
        a.nota3 = 10;
        
        //double mediaf = a.mediaFinal(a.nota1, a.nota2, a.nota3);
        
        System.out.println(a.mf);
        
        a.fim();
        
        
    }

}

CodePudding user response:

Your mediaFinal() method is only used once in your program, during Object Creation, so it's not surprising since the fields (int) nota1, nota2, nota3 are all zeros. So you need to use the mediaFinal() method again. For simplicity, you could use the no-argument version: double mf(){ return (( nota1 * 2.5) (nota2 * 2.5) (nota3 * 2)) / 7 ;} so later in main() method you can use: System.out.println(a.mf());

CodePudding user response:

mf as a variable get's calculated when creating

Aluno a = new Aluno();

when all the grades are 0.

  • Related