How to use ternary operator

2

good afternoon, I have a very specific question, what happens is that I want the user to enter a number and according to this establish whether the vehicle is a car or motorcycle, I think I already achieve it but not very well, after this I want that if the vehicle is auto then ask the model to the user the model, in the same way with the motorcycle, for example: if it is a car then I will ask it in the following way ("what is the model of the car:") , if it is motorcycle in this way ("what is the model of the motorcycle:"), I hope and you have managed to understand me, thank you very much in advance

import java.util.Scanner;

public class Vehiculo{

    //atributos

    int Tipo;
    String Placa;
    int Modelo;
    int Km;
    int Cilindraje;
    static final double BASEA = 50.000;
    static final double BASEB = 70.000;
    boolean decidir;
    String decicion;
    boolean decidir2;
    String decicion2;



    // PEDIR DATOS

    public void pedirDatos(){

        Scanner pedir = new Scanner(System.in);

        System.out.println("introduce un numero del 1 al 3 si tu vehiculo es auto o del 4 al infinito si tu vehiculo es moto");
        Tipo = pedir.nextInt();

        //SI "Tipo" es menor o igual a 3 "decidir" es true
        decidir = (Tipo <= 3);

        decicion = (decidir) ? "Auto" : "Moto";

        System.out.println("Tu vehiculo es: "+ decicion);

        decidir2 = (decicion == "Auto");

        decicion2 = (decidir2) ? System.out.println("Introduce el modelo del auto: );
    
asked by David 26.08.2017 в 19:40
source

3 answers

3

The best solution would be to assign the name of the model to a variable, evaluating the number entered using a ternary operator. That is:

    vehiculoTipo = (vehiculoId <= 3) ? "Auto" : "Moto";

Then, you can use vehiculoTipo in the output on the screen as follows:

    System.out.println("Introduce el modelo de tu " + vehiculoTipo);

That's it, with that the problem is solved, when it's a car, it will print this:

Introduce el modelo de tu Auto

and when it's a motorcycle, this:

Introduce el modelo de tu Moto

Let's see a complete example. Note that I have given more descriptive names to the variables, also implementing a naming convention. Otherwise you are quickly lost in the code and you do not know what to do with the variables.

    int vehiculoId;
    String vehiculoTipo;
    String vehiculoPlaca;
    int vehiculoModelo;
    int vehiculoKm;
    int vehiculoCilindraje;

    System.out.println("introduce un numero del 1 al 3 si tu vehiculo es auto o del 4 al infinito si tu vehiculo es moto: ");
    Scanner pedir = new Scanner(System.in);
    vehiculoId = pedir.nextInt();
    vehiculoTipo = (vehiculoId <= 3) ? "Auto" : "Moto";
    System.out.println("Introduce el modelo de tu " + vehiculoTipo);
    vehiculoModelo = pedir.nextInt();
    System.out.println("Has introducido un vehículo del tipo: " + vehiculoTipo + " / Modelo: " + vehiculoModelo);
    pedir.close();

Output test 1:

introduce un numero del 1 al 3 si tu vehiculo es auto o del 4 al infinito si tu vehiculo es moto: 
1
Introduce el modelo de tu Auto
1972
Has introducido un vehículo del tipo: Auto / Modelo: 1972

Output test 2:

introduce un numero del 1 al 3 si tu vehiculo es auto o del 4 al infinito si tu vehiculo es moto: 
8
Introduce el modelo de tu Moto
2018
Has introducido un vehículo del tipo: Moto / Modelo: 2018
    
answered by 27.06.2018 в 01:39
2

The ternary operation is correct:

decidir = (Tipo <= 3);

decicion = (decidir) ? "Auto" : "Moto";

You get a String value from the value of the variable boolean decidir .

But to print the value of Auto or Moto without using a if , you should use:

System.out.println("Tu vehiculo es: "+ ((decidir) ? "Auto" : "Moto"));

Another detail is that later you are making an incorrect comparison of String type values:

decidir2 = (decicion == "Auto");

You must use the .equals () method like this:

decidir2 = (decicion.equals("Auto"));

This way you would get a boolean value according to the comparison of String type values.

    
answered by 26.08.2017 в 20:03
0

The ternary operator serves as an expression that allows to return a value . That is, the use of the ternary operator is to assign a value to a variable. Example (based on your code):

decicion = (decidir) ? "Auto" : "Moto";

Keep in mind that the ternary operator does not replace a if . This is the problem you have here:

//mal uso de operador ternario
decicion2 = (decidir2) ? System.out.println("Introduce el modelo del auto: );

In these cases, it is best to use a if :

if (decidir) {
    System.out.println("Introduce el modelo del auto: );
    decicion2 = pedir.nextLine();
}
    
answered by 26.08.2017 в 20:03