It has two strategies and depends on what you want.
You must communicate the value through the method, step it as argument and I receive as parameter and return the changed value for who called. Something like that:
public class Main {
static Scanner entrada = new Scanner(System.in);
Aluno aluno = new Aluno();
String[] disciplinas = new String[5];
int numeroDisciplinas = 0;
numeroDisciplinas = cadastrarDisciplina(numeroDisciplinas);
}
public static int cadastrarDisciplina(int numeroDisciplinas) {
return numeroDisciplinas + 1; //espero que faça mais que isto, não faz sentido só ter essa linha
}
To be honest with you, this code doesn’t seem the most appropriate, but the whole code doesn’t seem the most appropriate. Then it may be wanted to create this variable outside of the methods and it having the scope of the class all methods can access it. But if you do it with one variable, you do it with the others, right?
static int numeroDisciplinas = 0;
static Scanner entrada = new Scanner(System.in);
static String[] disciplinas = new String[5];
public class Main {
Aluno aluno = new Aluno(); //espero que isto seja usado adequadamente, quem sabe deva estar fora também
cadastrarDisciplina(numeroDisciplinas);
}
public static void cadastrarDisciplina() {
numeroDisciplinas + 1;
}
I put in the Github for future reference.
It seems more appropriate, right? But I wouldn’t go so fast, it works, but in real code you’d hardly do that and you’re learning wrong. Of course, understanding the problem better may be until it can be useful in another scenario.
I still think all the code is bad and is just trying to get a problem because this code is bad, if you do it in a better way you wouldn’t have this problem. You can’t help more than that because you’d need to understand every detail of the problem. In general, people who are learning to program tend to have more problems understanding the problem than the programming itself. Focus on this.
Tried getters & setters ? https://stackoverflow.com/questions/1028967/simple-getter-setter-comments
– Luis Henrique
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero