Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何限制变量的值? proficial prof=新proficial(空,空); List profissional=new ArrayList(); 扫描仪sc=新的扫描仪(System.in); while(true){ 字符串comando=sc.next().toUpperCase(); 如果(comando.contentEquals(“RP”)){ 字符串categoriaPro=sc.next(); 如果(!categoriaPro.equals(“Medicina”)){ System.out.println(“分类不存在”); }如果(!categoriaPro.equals(“Enfermagem”)){ System.out.println(“分类不存在”); }如果(!categoriaPro.equals(“辅助”)){ System.out.println(“分类不存在”); } 字符串nomePro=sc.next(); 可可教授(专业、专业、分类); } if(comando.contentEquals(“SAIR”)){ 打破 } } 适用于(专业人士:专业人士){ System.out.println(pro); }_Java_List_Variables_Methods - Fatal编程技术网

Java 如何限制变量的值? proficial prof=新proficial(空,空); List profissional=new ArrayList(); 扫描仪sc=新的扫描仪(System.in); while(true){ 字符串comando=sc.next().toUpperCase(); 如果(comando.contentEquals(“RP”)){ 字符串categoriaPro=sc.next(); 如果(!categoriaPro.equals(“Medicina”)){ System.out.println(“分类不存在”); }如果(!categoriaPro.equals(“Enfermagem”)){ System.out.println(“分类不存在”); }如果(!categoriaPro.equals(“辅助”)){ System.out.println(“分类不存在”); } 字符串nomePro=sc.next(); 可可教授(专业、专业、分类); } if(comando.contentEquals(“SAIR”)){ 打破 } } 适用于(专业人士:专业人士){ System.out.println(pro); }

Java 如何限制变量的值? proficial prof=新proficial(空,空); List profissional=new ArrayList(); 扫描仪sc=新的扫描仪(System.in); while(true){ 字符串comando=sc.next().toUpperCase(); 如果(comando.contentEquals(“RP”)){ 字符串categoriaPro=sc.next(); 如果(!categoriaPro.equals(“Medicina”)){ System.out.println(“分类不存在”); }如果(!categoriaPro.equals(“Enfermagem”)){ System.out.println(“分类不存在”); }如果(!categoriaPro.equals(“辅助”)){ System.out.println(“分类不存在”); } 字符串nomePro=sc.next(); 可可教授(专业、专业、分类); } if(comando.contentEquals(“SAIR”)){ 打破 } } 适用于(专业人士:专业人士){ System.out.println(pro); },java,list,variables,methods,Java,List,Variables,Methods,这是我的主要任务 Profissional prof = new Profissional(null, null); List<Profissional> profissional = new ArrayList<Profissional>(); Scanner sc = new Scanner(System.in); while(true) { String comando = sc.next().toUpperCase(); if (comando.

这是我的主要任务

Profissional prof = new Profissional(null, null);
List<Profissional> profissional = new ArrayList<Profissional>();
Scanner sc = new Scanner(System.in);
while(true) {
    String comando = sc.next().toUpperCase();

    if (comando.contentEquals("RP")) {
        String categoriaPro = sc.next();
        if(!categoriaPro.equals("Medicina")) {
            System.out.println("Categoria inexistente");
        }else if(!categoriaPro.equals("Enfermagem")) {
            System.out.println("Categoria inexistente");
        }else if(!categoriaPro.equals("Auxiliar")) {
            System.out.println("Categoria inexistente");
        }
        String nomePro = sc.next();
        prof.NomeVerificacao(profissional , nomePro, categoriaPro);
    }


    if(comando.contentEquals("SAIR")) {
        break;
    }
}

for(Profissional pro : profissional) {
    System.out.println(pro);
}
公共类proficial{
私有字符串名称;
私有字符串范畴;
公共专业人员(字符串名称、字符串分类){
this.nome=nome;
this.categoria=categoria;
}
//接球手和接球手
可可豆(列表、字符串名称、字符串分类){
if(profissional.isEmpty()==true){
添加(新的专业术语(名词,分类));
}否则{
int i=0;
对于(;i
我试图创建一个对象,在这个例子中是一家医院的专业人员。 我们所做的是,不能有更多的专业人员使用相同的名称,但我不明白的是如何将“categoriaPro”的值限制为“Medicina”、“Enfermagem”和“Auxiliar”。 如果不是其中之一,我会打印消息“Categoria incexistent”。 但它不起作用。
任何主体都可以帮助???

将变量限制为某些值

此外,您需要在条件之后有一个AND语句,并且仅当它们都失败时才打印“Categoria inexistent”


上面检查的是不是它们中的任何一个。

没错,但无论如何它都会添加到列表中!如何在不运行list.add()的情况下重置main?您应该遵循Java命名约定:方法名称应该用camelCase编写,即
nomeverificaco
应该是
nomeverificaco
public class Profissional {
    private String nome;
    private String categoria;

    public Profissional(String nome, String categoria) {
        this.nome = nome;
        this.categoria = categoria;
    }
     // getters and setter

    public void NomeVerificacao(List<Profissional> profissional, String nome, String categoria) {
        if (profissional.isEmpty() == true) {
            profissional.add(new Profissional(nome, categoria));
        } else {
            int i = 0;
            for (; i < profissional.size(); i++) {
                if (profissional.get(i).nome.equals(nome)) {
                    System.out.println("Profissional existente.");
                    break;
                }
            }
            if (i == profissional.size()) {
                profissional.add(new Profissional(nome, categoria));
            }
        }
    }
}
if(!categoriaPro.equals("Medicina") && !categoriaPro.equals("Enfermagem") && !categoriaPro.equals("Auxiliar")) {
    System.out.println("Categoria inexistente");
}