Java - como impedir que se vuelva a introducir datos en el mismo switch

 
Vista:

como impedir que se vuelva a introducir datos en el mismo switch

Publicado por uriel (1 intervención) el 11/03/2019 02:24:26
hola buenas noches. soy nuevo en esto de la programacion y queria ver si me podrian ayudar a impedir que cuando introdusca valores dentro de un case y vuelva a seleccionar esa misma opcion ya no pueda introducir valores en ese case

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class Promedios {
    static int cont=0;
    static float acu;
    static Promedios promet=new Promedios();
    float p1,p2,p3,promedio,promfi;
    public float calcula(){
 
        p1=Float.parseFloat(JOptionPane.showInputDialog("ingrese promedio 1"));
        p2=Float.parseFloat(JOptionPane.showInputDialog("ingrese promedio 2"));
        p3=Float.parseFloat(JOptionPane.showInputDialog("ingrese promedio 3"));
        promedio=(p1+p2+p3)/3;
        JOptionPane.showMessageDialog(null, "tu pormdio es "+promedio);
        acu+=promedio;
        cont++;
 
        return promedio;
 
    }
    public float promf(){
 
 
        promfi=acu/cont;
        return promfi;
    }
 
    public static void main(String[] args) {
        int menu;
        do
        {
         menu=Integer.parseInt(JOptionPane.showInputDialog("Selecciona una materia\n"
                  + "1)Algebra Lineal\n"
                  + "2)P.O.O.\n"
                  + "3)Ingles\n"
                  + "4)Est.y Din\n"
                  + "5)Calculo\n"
                  + "6)Metrologia\n"
                  + "7)Elect. y Mag.\n"
                  + "8)Pensamiento Critico\n"
                  + "0)Salir\n"));
         switch(menu)
         {
             case 1: promet.calcula();
             break;
             case 2:promet.calcula();
             break;
             case 3:promet.calcula();
             break;
             case 4:promet.calcula();
             break;
             case 5:promet.calcula();
             break;
             case 6:promet.calcula();
             break;
             case 7:promet.calcula();
             break;
             case 8: promet.calcula();
             break;
             default:
 
 
         }
        }while(menu!=0);
 
        JOptionPane.showMessageDialog(null,"tu promedio es" +promet.promf());
 
    }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

como impedir que se vuelva a introducir datos en el mismo switch

Publicado por Billy Joel (875 intervenciones) el 11/03/2019 19:39:04
Un switch dinamico eh
Bueno se trata de preguntar si una propiedad ya tiene valor.
Está buena esta pregunta y lo que se me ha ocurrido para resolverlo es así:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
 
public class Promedios {
 
    public class Materia {
 
        private Integer id;
        private String nombre;
        private Double promedio;
 
        public Materia() {
            id = 0;
            nombre = "";
            promedio = 0.0;
        }
 
        public Materia(Integer id, String nombre) {
            this.id = id;
            this.nombre = nombre;
            promedio = 0.0;
        }
 
        /**
         * @return the id
         */
        public Integer getId() {
            return id;
        }
 
        /**
         * @param id the id to set
         */
        public void setId(Integer id) {
            this.id = id;
        }
 
        /**
         * @return the nombre
         */
        public String getNombre() {
            return nombre;
        }
 
        /**
         * @param nombre the nombre to set
         */
        public void setNombre(String nombre) {
            this.nombre = nombre;
        }
 
        /**
         * @return the promedio
         */
        public Double getPromedio() {
            return promedio;
        }
 
        /**
         * @param promedio the promedio to set
         */
        public void setPromedio(Double promedio) {
            this.promedio = promedio;
        }
    }
 
    private Map<Integer, Materia> materias = null;
 
    public Promedios() {
        materias = new HashMap();
        materias.put(1, new Materia(1, "Algebra lineal"));
        materias.put(2, new Materia(2, "P.O.O."));
        materias.put(3, new Materia(3, "Ingles"));
        materias.put(4, new Materia(4, "Est.y Din"));
        materias.put(5, new Materia(5, "Calculo"));
        materias.put(6, new Materia(6, "Metrologia"));
        materias.put(7, new Materia(7, "Elect. y Mag."));
        materias.put(8, new Materia(8, "Pensamiento Critico"));
    }
 
    public double calcula() {
        float p1 = Float.parseFloat(JOptionPane.showInputDialog("ingrese parcial 1"));
        float p2 = Float.parseFloat(JOptionPane.showInputDialog("ingrese parcial 2"));
        float p3 = Float.parseFloat(JOptionPane.showInputDialog("ingrese parcial 3"));
        double promedio = (p1 + p2 + p3) / 3.0;
        JOptionPane.showMessageDialog(null, "tu pormdio es " + promedio);
        return promedio;
    }
 
    public double promf() {
        double acu = 0;
        for (Integer key : materias.keySet()) {
            acu += materias.get(key).getPromedio();
        }
        return acu / materias.size();
    }
 
    public static void main(String[] args) {
        Promedios p = new Promedios();
        String message;
        Integer menu;
        do {
            message = "";
            for (Integer key : p.getMaterias().keySet()) {
                if (p.getMaterias().get(key).getPromedio() == 0.0) {
                    message += key + ")" + p.getMaterias().get(key).getNombre() + "\n";
                }
            }
            message += "0)Salir";
            String m = JOptionPane.showInputDialog(message);
            menu = m == null ? -1 : Integer.parseInt(m);
            if (menu == null || menu != 0) {
                if (!p.getMaterias().containsKey(menu) || p.getMaterias().get(menu).getPromedio() != 0.0) {
                    JOptionPane.showMessageDialog(null, "Opción invalida", "Advertencia", JOptionPane.WARNING_MESSAGE);
                } else {
                    p.getMaterias().get(menu).setPromedio(p.calcula());
                }
            }
        } while (menu != 0);
        JOptionPane.showMessageDialog(null, "tu promedio es" + p.promf());
    }
 
    /**
     * @return the materias
     */
    public Map<Integer, Materia> getMaterias() {
        return materias;
    }
 
    /**
     * @param materias the materias to set
     */
    public void setMaterias(Map<Integer, Materia> materias) {
        this.materias = materias;
    }
}

Se que tal vez es un poco avanzado, pero voy a pensar como simplificarlo...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar