简体   繁体   English

如何替换超过 16 个 if else-if 语句 Java

[英]how can replace more then 16 if else-if statement Java

Im trying to make a program that allows the client to input a String.我正在尝试制作一个允许客户端输入字符串的程序。 The string length should have 3 characters only and should contain the letters .字符串长度应该只有 3 个字符,并且应该包含字母。

My program have to pass through this table and check what this string refers to..我的程序必须通过这个表并检查这个字符串指的是什么..

Let's say the client passed this String "AUG", my program should show the name of this String which is "Met".假设客户端传递了这个字符串“AUG”,我的程序应该显示这个字符串的名称是“Met”。

I made a code, and it worked but it has more then 15 if else-if condition.我编写了一个代码,它可以工作,但它有超过 15 个 if else-if 条件。

My question is : Is there any other way to do it without using if else-if (or switch).我的问题是:有没有其他方法可以在不使用 if else-if(或 switch)的情况下做到这一点。 And does polymorphism work in this case ?多态性在这种情况下有效吗?

Have a look at HashMap看看HashMap

You can build your table with:您可以使用以下方法构建表格:

Map<String, String> table = new HashMap<>();
table.put("AUG", "Met");
table.put(...);

Then access your table using the user's input:然后使用用户的输入访问您的表:

if(table.containsKey(input)){
    return table.get(input);
}

I think I'd go about it with an enum personally (provided performance wasn't a significant concern):我想我会亲自进行enum (如果性能不是一个重要问题):

public enum Abbreviations {
    Ala("GCU", "GCC", "GCA", "GCG"),
    Arg("CGU", "CGC", "CGA", "CGG", "AGA", "AGG")
    // ...
    ;

    private final List<String> codons;

    private Abbreviations(final String... codons) {
        this.codons = Arrays.asList(codons);
    }


    public boolean contains(final String codon) {
        return this.codons.contains(codon);
    }
}

And then you can find their matching from the String using something like:然后您可以使用以下内容从String找到它们的匹配项:

public String find(final String codon) {
    for (final Abbreviations abb : Abbreviations.values()) {
        if (abb.contains(codon)) {
            return abb.name();
        }
    }

    throw new IllegalArgumentException("Unknown codon: '" + codon + "'");
}

You could try an Object Oriented Aproach:您可以尝试面向对象的方法:

//This is your representation of Codon
//Which has a name e.g. Alanine and an Abreviation object.
public class Codon {

        private String name;

        private Abreviation abreviation;

        public Codon(String name, Abreviation abreviation) {

            this.name = name;
            this.abreviation = abreviation;
            this.abreviation.addCodon(this);

        }

        @Override
        public String toString() {
            return "Codon [name=" + name + ", abreviation=" + abreviation + "]";
        }


    }

import java.util.ArrayList;
import java.util.List;
// This is a representation of an abreviation object
// Which has an abreviation: ALA;
// and the name of the abreviation "Alanine".
public class Abreviation {

    private String abreviation;
    private String name;
    private List<Codon> codons = new ArrayList<>();

    public Abreviation(String abreviation, String name) {
        super();
        this.abreviation = abreviation;
        this.name = name;

    }

    public boolean addCodon(Codon codon) {
        return this.codons.add(codon);
    }

    @Override
    public String toString() {
        return "Abreviation [abreviation=" + abreviation + ", name=" + name + "]";
    }

}


// Here is your program, where it's being build all the Codons structure with your respective Abbreviation.
public class App {

    public static void main(String[] args) {
        // This is abreviation, it'll will associated with the codon 
        Abreviation alanine = new Abreviation("Ala", "Alanine");
        // Here it's being build the codon CGU, which has abreviation alanine
        Codon GCU = new Codon("GCU", alanine);
        // Then using toString method it prints what have been done 
        System.out.println(GCU);

    }

}

You can put all of your codons into a List, so you can search and retrieve then.您可以将所有密码子放入一个列表中,以便您可以搜索和检索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM