【问题标题】:Counting object variable ocurrence with switch statement not working使用 switch 语句计算对象变量的出现不起作用
【发布时间】:2013-12-13 14:38:56
【问题描述】:

我只是在学习 Java,我正在尝试制作一个程序来计算程序中调用的区域或“cuadrante”有票或“multa”的次数,问题是这样的我这样做会返回奇怪的结果。例如,如果我为每个循环输入 cuadrante 1,我会得到 ​​p>

cuadrante1: 3 夸德兰特2:3 夸德兰特3:3 cuadrante4: 3

什么时候应该: 夸德兰特1:3 夸德兰特2:0 夸德兰特3:0 cuadrante4: 0

所以,我想知道这有什么问题。

希望你能帮助我。 谢谢

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multas;

/**
 *
 * @author sanikte
 */
public class Multas {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int cuadrante1 = 0, cuadrante2 = 0, cuadrante3 = 0, cuadrante4 = 0;

        NewMulta[] multa = new NewMulta[3];
        for (int i = 0; i < multa.length; i++) {
            multa[i] = new NewMulta();
            multa[i].changeCuadrante();
            multa[i].changeRegistroVehiculo();
            multa[i].changeVelocidadLimite();
            multa[i].changeVelocidadRegistrada();
            multa[i].imprimirReporte();
            multa[i].calcularInfraccion();

            switch (multa[i].cuadrante) {
                case 1:
                    cuadrante1++;
                case 2:
                    cuadrante2++;
                case 3:
                    cuadrante3++;
                case 4:
                    cuadrante4++;
                default:
                    break;
            }

        }
        System.out.println("Cuadrantes" + cuadrante1 + cuadrante2 + cuadrante3 + cuadrante4);

    }
}


    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multas;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 *
 * @author sanikte
 */
public class NewMulta {
    int cuadrante = 0;
    int velocidadLimite = 0;
    int velocidadRegistrada = 0;
    int infraccion = 0;

    String registroVehiculo = "No especificado";

     int changeCuadrante(){
        int newCuadrante;
        System.out.println("Ingrese el cuadrante en el que se ha presentado la multa:  1, 2, 3 o 4 y presione enter: \r");
        Scanner teclado = new Scanner(System.in);
        newCuadrante = teclado.nextInt();
        cuadrante = newCuadrante;
        return cuadrante;  
    }

    String changeRegistroVehiculo(){
        String newRegistroVehiculo;
        System.out.println("Ingrese el VIN \r");
        Scanner teclado = new Scanner(System.in);
        newRegistroVehiculo = teclado.next();
        registroVehiculo = newRegistroVehiculo;
        return registroVehiculo; 
    }

    int changeVelocidadLimite(){
        int newVelLimite;
        System.out.println("Ingrese velocidad límite del área: ");
        Scanner teclado = new Scanner(System.in);
        newVelLimite = teclado.nextInt();
        velocidadLimite = newVelLimite;
        return velocidadLimite;
    }

    int changeVelocidadRegistrada(){
        int newVelocidadRegistrada;
        System.out.println("Ingrese velocidad en la que iba el conductor: ");
        Scanner teclado = new Scanner(System.in);
        newVelocidadRegistrada = teclado.nextInt();
        velocidadRegistrada = newVelocidadRegistrada;
        return velocidadRegistrada;
    }

    int calcularInfraccion(){

        int diferencia;
        diferencia = velocidadRegistrada - velocidadLimite;
        infraccion = diferencia * 1250;
        System.out.println(infraccion);
        return infraccion;
    }





    void imprimirReporte(){
    System.out.println ("La información es: \r Cuadrante: "+ cuadrante +"\r Velocidad límite: " 
            + velocidadLimite + "\r Velocidad registrada: "+ velocidadRegistrada + "\r VIN: "+registroVehiculo+"\r Cantidad a pagar: "+ infraccion );
    }


}

【问题讨论】:

    标签: java variables switch-statement counting


    【解决方案1】:

    失败。

    您必须在每个案例之后添加break。否则,它会增加所有变量。

    switch (multa[i].cuadrante) {
                case 1:
                    cuadrante1++;
                     break;
                --
                default:
                    break;
            }
    

    【讨论】:

    • @user2355171 很高兴,它有帮助
    猜你喜欢
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    相关资源
    最近更新 更多