gpt4 book ai didi

java - 重构大量 if 语句

转载 作者:行者123 更新时间:2023-12-01 12:22:07 26 4
gpt4 key购买 nike

所以我有一个制作随机名称生成器的项目,目前我有通过以下方式选择的前缀和后缀:A) 该人名字中的字母数量,以及B) 他们名字的第一个和最后一个字母。

代码当前按预期运行,我只是想改进代码并希望删除包含数千个 if 语句的数百行。

import java.util.*;
public class ranName
{
public static void main(String[] args)
{
String input, firstName, lastName;
String firstPre, firstSuff, lastPre, lastSuff, lSuffMean, lPreMean, fLastLet, fFirstLet, lLastLet, lFirstLet;

Scanner sc = new Scanner(System.in);

System.out.println("Welcome to the Lord Of The Rings Elf name Generator!");
System.out.println("------------------------------------------------");
System.out.println("");
System.out.print("First Name: ");
firstName = sc.nextLine();

while (true)
{
System.out.println("------------------------------------------------");
System.out.println("You inserted " + firstName);
System.out.println("Are you sure?");
System.out.print("Y/N: ");
input = sc.nextLine();
System.out.println("------------------------------------------------");
if (input.equalsIgnoreCase("Y"))
{
break;
}
else if (input.equalsIgnoreCase("N"))
{
System.out.print("First Name: ");
firstName = sc.nextLine();
}
else
{
System.out.println("Oh well, you tried. Here's another go at it.");
}
}

System.out.print("Last Name: ");
lastName = sc.nextLine();

while (true)
{
System.out.println("------------------------------------------------");
System.out.println("You inserted " + lastName);
System.out.println("Are you sure?");
System.out.print("Y/N: ");
input = sc.nextLine();
System.out.println("------------------------------------------------");
if (input.equalsIgnoreCase("Y"))
{
break;
}
else if (input.equalsIgnoreCase("N"))
{
System.out.print("Last Name: ");
lastName = sc.nextLine();
}
else
{
System.out.println("Oh well, you tried. Here's another go at it.");
}
}
System.out.print("Your Elf Name: ");

firstPre = preGet(firstName);
firstSuff = suffGet(firstName);


lastPre = housePreGet(lastName);
lastSuff = houseSuffGet(lastName);

lPreMean = preMean(lastPre);
lSuffMean = suffMean(lastSuff);

fLastLet = String.valueOf(firstPre.charAt(firstPre.length()-1));
fFirstLet = String.valueOf(firstSuff.charAt(0));

lLastLet = String.valueOf(lastPre.charAt(lastPre.length()-1));
lFirstLet = String.valueOf(lastSuff.charAt(0));

if (fFirstLet.equals(fLastLet))
{
firstSuff = (firstSuff.substring(1));
}

if (lFirstLet.equals(lLastLet))
{
lastSuff = (lastSuff.substring(1));
}

System.out.println(firstPre + firstSuff + " " + lastPre + lastSuff);
System.out.println("");
System.out.println("------------------------------------------------");
System.out.println("The House Name (lastname) Translates to: " + lPreMean + " " + lSuffMean);
System.out.println("------------------------------------------------");
}

public static String preGet(String fN)
{
String[] namePre;
String fNN, fL;
fNN = fN.trim();
int fnCount = fNN.length();
fL = String.valueOf(fNN.charAt(0));
namePre = new String[53];
namePre[0] = "PlaceHolder";
namePre[1] = "Ael";
namePre[2] = "Aer";
namePre[3] = "Bael";
namePre[4] = "Bes";
namePre[5] = "Cael";
namePre[6] = "Cor";
namePre[7] = "Dae";
namePre[8] = "Dre";
namePre[9] = "Eil";
namePre[10] = "Ev";
namePre[11] = "Fir";
namePre[12] = "Fis";
namePre[13] = "Gael";
namePre[14] = "Gil";
namePre[15] = "Ha";
namePre[16] = "Hu";
namePre[17] = "Ia";
namePre[18] = "Il";
namePre[19] = "Ja";
namePre[20] = "Jar";
namePre[21] = "Kan";
namePre[22] = "Kor";
namePre[23] = "La";
namePre[24] = "Lue";
namePre[25] = "Mai";
namePre[26] = "Mara";
namePre[27] = "Na";
namePre[28] = "Nim";
namePre[29] = "Ol";
namePre[30] = "Onn";
namePre[31] = "Py";
namePre[32] = "Pael";
namePre[33] = "Qu";
namePre[34] = "Qi";
namePre[35] = "Rum";
namePre[36] = "Rua";
namePre[37] = "Sae";
namePre[38] = "Sha";
namePre[39] = "Tahl";
namePre[40] = "Thro";
namePre[41] = "Ul";
namePre[42] = "Uon";
namePre[43] = "Ver";
namePre[44] = "Vil";
namePre[45] = "Wuo";
namePre[46] = "Waal";
namePre[47] = "Xae";
namePre[48] = "Xen";
namePre[49] = "Ya";
namePre[50] = "Yae";
namePre[51] = "Za";
namePre[52] = "Zy";

if (fnCount % 2 == 0)
{
if (fL.equalsIgnoreCase("A"))
{
return namePre[1];
}
else if (fL.equalsIgnoreCase("B"))
{
return namePre[3];
}
else if (fL.equalsIgnoreCase("C"))
{
return namePre[5];
}
else if (fL.equalsIgnoreCase("D"))
{
return namePre[7];
}
else if (fL.equalsIgnoreCase("E"))
{
return namePre[9];
}
else if (fL.equalsIgnoreCase("F"))
{
return namePre[11];
}
else if (fL.equalsIgnoreCase("G"))
{
return namePre[13];
}
else if (fL.equalsIgnoreCase("H"))
{
return namePre[15];
}
else if (fL.equalsIgnoreCase("I"))
{
return namePre[17];
}
else if (fL.equalsIgnoreCase("J"))
{
return namePre[19];
}
else if (fL.equalsIgnoreCase("K"))
{
return namePre[21];
}
else if (fL.equalsIgnoreCase("L"))
{
return namePre[23];
}
else if (fL.equalsIgnoreCase("M"))
{
return namePre[25];
}
else if (fL.equalsIgnoreCase("N"))
{
return namePre[27];
}
else if (fL.equalsIgnoreCase("O"))
{
return namePre[29];
}
else if (fL.equalsIgnoreCase("P"))
{
return namePre[31];
}
else if (fL.equalsIgnoreCase("Q"))
{
return namePre[33];
}
else if (fL.equalsIgnoreCase("R"))
{
return namePre[35];
}
else if (fL.equalsIgnoreCase("S"))
{
return namePre[37];
}
else if (fL.equalsIgnoreCase("T"))
{
return namePre[39];
}
else if (fL.equalsIgnoreCase("U"))
{
return namePre[41];
}
else if (fL.equalsIgnoreCase("V"))
{
return namePre[43];
}
else if (fL.equalsIgnoreCase("W"))
{
return namePre[45];
}
else if (fL.equalsIgnoreCase("X"))
{
return namePre[47];
}
else if (fL.equalsIgnoreCase("Y"))
{
return namePre[49];
}
else if (fL.equalsIgnoreCase("Z"))
{
return namePre[51];
}
}
else
{
if (fL.equalsIgnoreCase("A"))
{
return namePre[2];
}
else if (fL.equalsIgnoreCase("B"))
{
return namePre[4];
}
else if (fL.equalsIgnoreCase("C"))
{
return namePre[6];
}
else if (fL.equalsIgnoreCase("D"))
{
return namePre[8];
}
else if (fL.equalsIgnoreCase("E"))
{
return namePre[10];
}
else if (fL.equalsIgnoreCase("F"))
{
return namePre[12];
}
else if (fL.equalsIgnoreCase("G"))
{
return namePre[14];
}
else if (fL.equalsIgnoreCase("H"))
{
return namePre[16];
}
else if (fL.equalsIgnoreCase("I"))
{
return namePre[18];
}
else if (fL.equalsIgnoreCase("J"))
{
return namePre[20];
}
else if (fL.equalsIgnoreCase("K"))
{
return namePre[22];
}
else if (fL.equalsIgnoreCase("L"))
{
return namePre[24];
}
else if (fL.equalsIgnoreCase("M"))
{
return namePre[26];
}
else if (fL.equalsIgnoreCase("N"))
{
return namePre[28];
}
else if (fL.equalsIgnoreCase("O"))
{
return namePre[30];
}
else if (fL.equalsIgnoreCase("P"))
{
return namePre[32];
}
else if (fL.equalsIgnoreCase("Q"))
{
return namePre[34];
}
else if (fL.equalsIgnoreCase("R"))
{
return namePre[36];
}
else if (fL.equalsIgnoreCase("S"))
{
return namePre[38];
}
else if (fL.equalsIgnoreCase("T"))
{
return namePre[40];
}
else if (fL.equalsIgnoreCase("U"))
{
return namePre[42];
}
else if (fL.equalsIgnoreCase("V"))
{
return namePre[44];
}
else if (fL.equalsIgnoreCase("W"))
{
return namePre[46];
}
else if (fL.equalsIgnoreCase("X"))
{
return namePre[48];
}
else if (fL.equalsIgnoreCase("Y"))
{
return namePre[50];
}
else if (fL.equalsIgnoreCase("Z"))
{
return namePre[52];
}
}
return "";
}
public static String suffGet(String fN)
{
String[] nameSuff;
String fNN, fL;
fNN = fN.trim();
int fnCount = fNN.length();
fL = String.valueOf(fNN.charAt(fNN.length()-1));
nameSuff = new String[53];
nameSuff[0] = "placeholder";
nameSuff[1] = "ae";
nameSuff[2] = "aith";
nameSuff[3] = "brar";
nameSuff[4] = "bael";
nameSuff[5] = "cael";
nameSuff[6] = "con";
nameSuff[7] = "drimme";
nameSuff[8] = "dul";
nameSuff[9] = "emar";
nameSuff[10] = "evar";
nameSuff[11] = "fel";
nameSuff[12] = "faen";
nameSuff[13] = "gael";
nameSuff[14] = "gin";
nameSuff[15] = "hal";
nameSuff[16] = "har";
nameSuff[17] = "ii";
nameSuff[18] = "im";
nameSuff[19] = "jin";
nameSuff[20] = "jaal";
nameSuff[21] = "ki";
nameSuff[22] = "kas";
nameSuff[23] = "lian";
nameSuff[24] = "lihn";
nameSuff[25] = "mah";
nameSuff[26] = "'mek";
nameSuff[27] = "nes";
nameSuff[28] = "'nil";
nameSuff[29] = "onna";
nameSuff[30] = "oth";
nameSuff[31] = "pae";
nameSuff[32] = "pek";
nameSuff[33] = "'que";
nameSuff[34] = "quis";
nameSuff[35] = "ruil";
nameSuff[36] = "reth";
nameSuff[37] = "san";
nameSuff[38] = "sel";
nameSuff[39] = "thal";
nameSuff[40] = "thus";
nameSuff[41] = "ual";
nameSuff[42] = "uath";
nameSuff[43] = "vain";
nameSuff[44] = "vin";
nameSuff[45] = "wyn";
nameSuff[46] = "waal";
nameSuff[47] = "'xe";
nameSuff[48] = "'xol";
nameSuff[49] = "yth";
nameSuff[50] = "yl";
nameSuff[51] = "zair";
nameSuff[52] = "zara";

if (fnCount % 2 != 0)
{
if (fL.equalsIgnoreCase("A"))
{
return nameSuff[1];
}
else if (fL.equalsIgnoreCase("B"))
{
return nameSuff[3];
}
else if (fL.equalsIgnoreCase("C"))
{
return nameSuff[5];
}
else if (fL.equalsIgnoreCase("D"))
{
return nameSuff[7];
}
else if (fL.equalsIgnoreCase("E"))
{
return nameSuff[9];
}
else if (fL.equalsIgnoreCase("F"))
{
return nameSuff[11];
}
else if (fL.equalsIgnoreCase("G"))
{
return nameSuff[13];
}
else if (fL.equalsIgnoreCase("H"))
{
return nameSuff[15];
}
else if (fL.equalsIgnoreCase("I"))
{
return nameSuff[17];
}
else if (fL.equalsIgnoreCase("J"))
{
return nameSuff[19];
}
else if (fL.equalsIgnoreCase("K"))
{
return nameSuff[21];
}
else if (fL.equalsIgnoreCase("L"))
{
return nameSuff[23];
}
else if (fL.equalsIgnoreCase("M"))
{
return nameSuff[25];
}
else if (fL.equalsIgnoreCase("N"))
{
return nameSuff[27];
}
else if (fL.equalsIgnoreCase("O"))
{
return nameSuff[29];
}
else if (fL.equalsIgnoreCase("P"))
{
return nameSuff[31];
}
else if (fL.equalsIgnoreCase("Q"))
{
return nameSuff[33];
}
else if (fL.equalsIgnoreCase("R"))
{
return nameSuff[35];
}
else if (fL.equalsIgnoreCase("S"))
{
return nameSuff[37];
}
else if (fL.equalsIgnoreCase("T"))
{
return nameSuff[39];
}
else if (fL.equalsIgnoreCase("U"))
{
return nameSuff[41];
}
else if (fL.equalsIgnoreCase("V"))
{
return nameSuff[43];
}
else if (fL.equalsIgnoreCase("W"))
{
return nameSuff[45];
}
else if (fL.equalsIgnoreCase("X"))
{
return nameSuff[47];
}
else if (fL.equalsIgnoreCase("Y"))
{
return nameSuff[49];
}
else if (fL.equalsIgnoreCase("Z"))
{
return nameSuff[51];
}
}
else
{
if (fL.equalsIgnoreCase("A"))
{
return nameSuff[2];
}
else if (fL.equalsIgnoreCase("B"))
{
return nameSuff[4];
}
else if (fL.equalsIgnoreCase("C"))
{
return nameSuff[6];
}
else if (fL.equalsIgnoreCase("D"))
{
return nameSuff[8];
}
else if (fL.equalsIgnoreCase("E"))
{
return nameSuff[10];
}
else if (fL.equalsIgnoreCase("F"))
{
return nameSuff[12];
}
else if (fL.equalsIgnoreCase("G"))
{
return nameSuff[14];
}
else if (fL.equalsIgnoreCase("H"))
{
return nameSuff[16];
}
else if (fL.equalsIgnoreCase("I"))
{
return nameSuff[18];
}
else if (fL.equalsIgnoreCase("J"))
{
return nameSuff[20];
}
else if (fL.equalsIgnoreCase("K"))
{
return nameSuff[22];
}
else if (fL.equalsIgnoreCase("L"))
{
return nameSuff[24];
}
else if (fL.equalsIgnoreCase("M"))
{
return nameSuff[26];
}
else if (fL.equalsIgnoreCase("N"))
{
return nameSuff[28];
}
else if (fL.equalsIgnoreCase("O"))
{
return nameSuff[30];
}
else if (fL.equalsIgnoreCase("P"))
{
return nameSuff[32];
}
else if (fL.equalsIgnoreCase("Q"))
{
return nameSuff[34];
}
else if (fL.equalsIgnoreCase("R"))
{
return nameSuff[36];
}
else if (fL.equalsIgnoreCase("S"))
{
return nameSuff[38];
}
else if (fL.equalsIgnoreCase("T"))
{
return nameSuff[40];
}
else if (fL.equalsIgnoreCase("U"))
{
return nameSuff[42];
}
else if (fL.equalsIgnoreCase("V"))
{
return nameSuff[44];
}
else if (fL.equalsIgnoreCase("W"))
{
return nameSuff[46];
}
else if (fL.equalsIgnoreCase("X"))
{
return nameSuff[48];
}
else if (fL.equalsIgnoreCase("Y"))
{
return nameSuff[50];
}
else if (fL.equalsIgnoreCase("Z"))
{
return nameSuff[52];
}
return "";
}
return "";
}

最佳答案

使用 Map 的实现。让键为字母表中的字母,值为您当前存储在 namePre 数组中的内容。这种方法还可以让您省去数组,因为您的 map 充当存储和检索的手段。

以您的 preGet 方法为例。您可以以更紧凑的方式实现相同的目标,而不是编写所有这些条件,如下所示:

firstEvenPre = new HashMap<String, String>();

// some code to load up your prefixes

public static String preGet(String fN)
{
String[] namePre;
String fNN, fL;
fNN = fN.trim();
int fnCount = fNN.length();
fL = String.valueOf(fNN.charAt(0));

return (String)firstEvenPre.get(fL);
}

尝试代码时要谨慎,我没有测试它,而且我最近一直在编写 Ruby,所以我可能有一些脑雾。

关于java - 重构大量 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599803/

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