gpt4 book ai didi

java - 如何避免重复?

转载 作者:行者123 更新时间:2023-11-29 10:13:33 27 4
gpt4 key购买 nike

<分区>

我在做一个 Java 教程,我总是读到我必须尽量不要重复,我注意到这是非常重复的;因此,如果有人可以给我一些提示以减少重复或以某种方式更好,那就太好了。谢谢 ;)(这不是本教程的一部分,我制作它只是为了好玩,因为我在学校学习科学)

  1. Run.java 文件:

    package scientificFormula;

    public class Run {

    public static void main(String[] args) {
    Formula formula = new Formula();

    formula.compound1 = args[0];
    formula.compound2 = args[1];

    String theFormula = formula.createFormula();
    System.out.println("Molecule: " + args[0] + " " + args[1] + " = "
    + theFormula);
    }

    }
  2. Formula.java 文件:

    package scientificFormula;

    import java.util.HashMap;
    import java.util.Map;

    public class Formula {
    String compound1;
    String compound2;
    static private Map<String, String> map = new HashMap<String, String>();

    static private void initiateIons() {
    // 1+
    map.put("Hydrogen", "H^1+");
    map.put("Lithium", "Li^1+");
    map.put("Sodium", "Na^1+");
    map.put("Potassium", "K^1+");
    map.put("Rubidium", "Rb^1+");
    // 2+
    map.put("Magnesium", "Mg^2+");
    map.put("Calcium", "Ca^2+");
    map.put("Strontium", "Sr^2+");
    // 3+
    map.put("Aluminium", "Al^3+");
    // 3-
    map.put("Nitrogem", "N^-3");
    map.put("Phosphorus", "P^-3");
    // 2-
    map.put("Oxygen", "O^-2");
    map.put("Sulfur", "S^-2");
    map.put("Selenium", "Se^-2");
    // 1-
    map.put("Fluorine", "F^-1");
    map.put("Chlorine", "Cl^-1");
    map.put("Bromine", "Br^-1");
    map.put("Iodine", "I^-1");
    }

    String createFormula() {
    initiateIons();

    // Example1: Input = Calcium Iodine:
    // 2x + -1y = 0
    // x = 1 and y = 2
    // Output = CaI2
    //
    // Example2: Input = Sulfur Iodine
    // Output = Molecule: Sulfur Iodine = SI2

    String symbol1 = map.get(compound1);
    String symbol2 = map.get(compound2);

    int charge1 = Integer.parseInt(symbol1.replace("+", "").substring(
    symbol1.length() - 2));
    int charge2 = Integer.parseInt(symbol2.replace("+", "").substring(
    symbol2.length() - 2));

    String letter1 = null;
    String letter2 = null;

    if (symbol1.length() == 5) {
    letter1 = symbol1.substring(0, 2);
    } else if (symbol1.length() == 4) {
    letter1 = symbol1.substring(0, 1);
    }
    if (symbol2.length() == 5) {
    letter2 = symbol2.substring(0, 2);
    } else if (symbol2.length() == 4) {
    letter2 = symbol2.substring(0, 1);
    }

    int possitive1 = (int) Math.sqrt(charge1 * charge1);
    int possitive2 = (int) Math.sqrt(charge2 * charge2);

    if ((possitive1 == 1) & (possitive2 == 1)) {
    return letter1 + letter2;
    } else if (possitive1 == 1) {
    return letter1 + possitive2 + letter2;
    } else if (possitive2 == 1) {
    return letter1 + letter2 + possitive1;
    }
    if (possitive1 == 0) {
    possitive1 = -(charge1);
    }

    if (possitive2 == 0) {
    possitive2 = -(charge2);
    }

    return letter1 + possitive2 + letter2 + possitive1;
    }
    }

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com