【问题标题】:i need find the instance of an attribute that the object is into an array我需要找到对象在数组中的属性实例
【发布时间】:2018-11-12 12:52:05
【问题描述】:

我不知道如何在切换到 for 的阵列客户端中找到摩托罗拉的实例。练习是这样的:购买摩托罗拉手机的第一个客户的姓名和身份。在案例 2 中,也许我需要一个条件,比如 if 或一段时间,但我不知道该怎么做。再次,对不起我的英语,谢谢!!!

package principal1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import recursos.Cliente;

public class Principal1 {

    public static void main(String[] args) {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int opc = 0;
        double ventasLG = 0.0;
        int ventasKyocera = 0;
        String nombre = "";
        Cliente clientes[] = new Cliente[5];

        for (int i = 0; i < clientes.length; i++) {

            clientes[i] = new Cliente(nombre);

            String marcaCelular = "";
            int celular = 0;

            System.out.println("Ingrese marca del celular:\n1.-Nokia\n2.-Motorola" + "\n3.-LG\n4.-Kyocera");
            celular = Integer.parseInt(in.readLine());
            switch (celular) {
            case 1:
                clientes[i].setMarcaCelular("Nokia");
                System.out.println("Ingrese nombre del cliente");
                nombre = in.readLine();
                System.out.println("Ingrese cedula de identidad");
                int cedulaIdentidad = Integer.parseInt(in.readLine());
                System.out.println("Ingrese precio del celular");
                int precioCelular = Integer.parseInt(in.readLine());
                clientes[i] = new Cliente(nombre, cedulaIdentidad, marcaCelular, precioCelular);
                break;
            case 2:
                clientes[i].setMarcaCelular("Motorola");
                System.out.println("Ingrese nombre del cliente");
                nombre = in.readLine();
                System.out.println("Ingrese cedula de identidad");
                cedulaIdentidad = Integer.parseInt(in.readLine());
                System.out.println("Ingrese precio del celular");
                precioCelular = Integer.parseInt(in.readLine());
                clientes[i] = new Cliente(nombre, cedulaIdentidad, marcaCelular, precioCelular);

                break;
            case 3:
                clientes[i].setMarcaCelular("LG");
                System.out.println("Ingrese nombre del cliente");
                nombre = in.readLine();
                System.out.println("Ingrese cedula de identidad");
                cedulaIdentidad = Integer.parseInt(in.readLine());
                System.out.println("Ingrese precio del celular");
                precioCelular = Integer.parseInt(in.readLine());
                clientes[i] = new Cliente(nombre, cedulaIdentidad, marcaCelular, precioCelular);
                // 2
                ventasLG++;
                break;
            case 4:
                clientes[i].setMarcaCelular("Kyocera");
                System.out.println("Ingrese nombre del cliente");
                nombre = in.readLine();
                System.out.println("Ingrese cedula de identidad");
                cedulaIdentidad = Integer.parseInt(in.readLine());
                System.out.println("Ingrese precio del celular");
                precioCelular = Integer.parseInt(in.readLine());
                if (precioCelular >= 300000) {
                    ventasKyocera++;
                }
                clientes[i] = new Cliente(nombre, cedulaIdentidad, marcaCelular, precioCelular);
                break;
            default:
                System.out.println("Opcion incorrecta");
            }
        }
    }
}

【问题讨论】:

  • 你好。请澄清您的具体问题或添加其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。请参阅How to Ask 页面以获得澄清此问题的帮助。
  • 我需要购买摩托罗拉的第一个客户的打印名称和 cedulaIdentidad,所以我如何找到客户,她的 marcaCelular 是摩托罗拉,是第一个实例,我需要她的名字和 cedulaIdentidad。我为每个 marcaCelular 进行切换并创建一个类 Cliente 用于实例化客户端,

标签: java arrays methods


【解决方案1】:

你可以有一个boolean motorola = false,在case 2里面放一个

if(!motorola) {
    System.out.println("print first Motorola client ...");
    motorola = true;
}

或者在你拥有所有客户之后:

每次客户购买手机时,他的信息都会保存在客户数组中。这是按时间顺序存储的,因此第一个购买摩托罗拉的人将在下一个购买同一部手机的人之前。然后你只需要迭代clientes数组并打印第一个购买摩托罗拉的Cliente。

for(int i = 0; i < clientes.length; i++) {
    if(clientes[i].getMarcaCelular().equals("Motorola")){
        System.out.println(
            clientes[i].getNombre() + " , " + 
            clientes[i].getCedulaIdentidad()
        );
        break;
    }
}

另外,尝试用英文编写代码作为惯例。我确定你不想阅读Chinese typed code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-09
    • 2018-03-06
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多