-1
In this excerpt I got the expected result, which was not close the program after the error message if the user enters with wrong data. However, even if I’m in the 10th, it resets to the first. So here comes the question, is there anything I can do in this excerpt so that only ask the die again from pm with invalid die?
//Entrada de dados
while(true) { //Solução para o programa não fechar após mensagem de erro
try //Inicio trycatch
{
pm1 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 1ª mala? \n Insira abaixo:"));
pm2 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 2ª mala? \n Insira abaixo:"));
pm3 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 3ª mala? \n Insira abaixo:"));
pm4 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 4ª mala? \n Insira abaixo:"));
pm5 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 5ª mala? \n Insira abaixo:"));
pm6 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 6ª mala? \n Insira abaixo:"));
pm7 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 7ª mala? \n Insira abaixo:"));
pm8 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 8ª mala? \n Insira abaixo:"));
pm9 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 9ª mala? \n Insira abaixo:"));
pm10 = Float.parseFloat(JOptionPane.showInputDialog("Quantos Kg tem a 10ª mala? \n Insira abaixo:"));
break;
} catch (NumberFormatException e)
{
JOptionPane.showMessageDialog ( null, "Digite um peso válido!\n"
+ "Tente usar .(ponto) para números decimais.\n"
+ "Não use letras, apenas números.");
}
}
This code is being made unprofessional. Why not store the number of bags in an integer array and then with a "foreach" cycle, input the values? It was more professional code. If you have questions I can post an example
– BarrosKing
@Barrosking I understand, is that I am starting in the course, it is only the fifth week, the challenge in question was reaching, with the tools that we have learned so far, is that I would like to improve this question of the reset of values, and I do not quite know how to do this, because I had to put 10 variables for guards these pms, being that I imagine that a counter would be more efficient.
– Patrik Rufino
An answer was placed with the purpose spoken by me, if you do not understand anything you may ask!
– BarrosKing