My goal is that after I run one of the menu options and say that I do not want to continue on it, I can insert another option (or the same if you want to go back).
My code executes the first option that I type but does not pick up the second, just continue to run the code. Ex: if I put that I want to launch, and then say that I do not want to release notes of another student, he asks me the question of what I want to do however regardless of my answer he executes the second case "notes". Here is the code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Entrega {
public static void main(String[] args) {
String nomes [] = new String[100];
double notas [][] = new double [100][5];
String resposta = "S";
double media = 0;
double soma = 0;
int qtdnomes = 0;
String turma = "";
String resposta1 = "s";
String resposta2 = "s";
Scanner input = new Scanner (System.in);
System.out.println("O que deseja fazer? LANCAR notas, ver NOTAS, ver RESULTADOS, SAIR?" );
String opcao = input.nextLine();
while (resposta.equalsIgnoreCase("s")){
switch (opcao) {
case "lancar":
while(resposta1.equalsIgnoreCase("S")){
for(int i=0;i<=nomes.length;i++) {
input = new Scanner (System.in);
System.out.println("Qual o nome da turma?");
turma = input.nextLine();
input = new Scanner (System.in);
System.out.println("Qual o nome do aluno?");
nomes[i] = input.nextLine();
qtdnomes=qtdnomes+1;
for(int j=0;j<=3;j++) {
System.out.println("Qual a nota "+j);
notas[i][j]=input.nextDouble();
soma=soma+notas[i][j];
media=soma/4;
notas[i][4] = media;
}
System.out.println("Deseja lançar notas de outro aluno? S/N?");
resposta1 = input.next();
media=0;
soma=0;
input=null;
if(resposta1.equalsIgnoreCase("n")) {
resposta="n";
break;
}
}
try{
File medias = new File(turma);
FileWriter gravador = new FileWriter(medias,true);
for(int p=0;p<qtdnomes;p++){
gravador.write(nomes[p]+System.lineSeparator());
gravador.write(Arrays.toString(notas[p])+System.lineSeparator());
gravador.flush();
}
}
catch(IOException e){
System.err.printf("Erro na gravação do arquivo: %s.\n",e.getMessage());
}
input = new Scanner (System.in);
System.out.println("O que deseja fazer? LANCAR notas, ver NOTAS, ver RESULTADOS, SAIR?" );
opcao = input.nextLine();
}
case "notas":
while(resposta2.equalsIgnoreCase("s")) {
String nomearquivo="";
input = new Scanner (System.in);
System.out.println("Nome do arquivo de texto");
nomearquivo = input.next();
try {
FileReader arquivo = new FileReader(nomearquivo);
BufferedReader leitor = new BufferedReader(arquivo);
String linha;
while((linha = leitor.readLine())!=null){
System.out.println(linha);
}
input = new Scanner (System.in);
System.out.println("Deseja consultar outra turma? S/N?");
resposta2=input.nextLine();
if (resposta2.equalsIgnoreCase("n")) {
break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}