gpt4 book ai didi

java - 从输入的第一个字母调用字符串输入的输出

转载 作者:行者123 更新时间:2023-12-02 05:20:43 25 4
gpt4 key购买 nike

我正在尝试制作一个程序,该程序将使用输入名称,然后根据名字和姓氏的第一个字母,所以如果名称是鲍勃·马利,它将采用 B 并从数组 letter1 中获取输出那么它也会获取 M 并从数组字母 2 中获取输出。

    import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Starwars

{
static Map<String, String> firstNameMap;}firstNameMap = new HashMap<>();
static {
firstNameMap.put("A", "Cho");
firstNameMap.put("B", "R2");
firstNameMap.put("C", "C-3po");
firstNameMap.put("D", "Yod");
firstNameMap.put("E", "Nas");
firstNameMap.put("F", "Slea");
firstNameMap.put("G", "Jan");
firstNameMap.put("H", "Zhur");
firstNameMap.put("I", "Boba");
firstNameMap.put("J", "Thre");
firstNameMap.put("K", "Bib");
firstNameMap.put("L", "Kit");
firstNameMap.put("M", "Kyp");
firstNameMap.put("N", "Gonk");
firstNameMap.put("O", "Zlung");
firstNameMap.put("P", "Adi");
firstNameMap.put("Q", "Nat");
firstNameMap.put("R", "Ru");
firstNameMap.put("S", "Cla");
firstNameMap.put("T", "Kir");
firstNameMap.put("U", "Obi");
firstNameMap.put("V", "Ken");
firstNameMap.put("W", "Ziro");
firstNameMap.put("X", "Zev");
firstNameMap.put("Y", "Tavion");
firstNameMap.put("Z", "Jar");

public static void main ( String[] args )
{
String[] planet = null, rank = null, rebbelion = null, letter1 = null, letter2= null , Map , HashMap;
String firstName, lastName ,name2 , side , location, fistNameMap;

Scanner input = new Scanner( System.in ); //scanner initilized


}

/* letter1 = new String[25];//Name generation array uses first letter of first name
letter1 [0] = "Cho";
letter1 [1] = "R2";
letter1 [2] = "C-3po";
letter1 [3] = "Yod";
letter1 [4] = "Nas";
letter1 [5] = "Slea";
letter1 [6] = "Jan";
letter1 [7] = "Zhur";
letter1 [8] = "Boba";
letter1 [9] = "Thre";
letter1 [10] = "Bib";
letter1 [11] = "Kit";
letter1 [12] = "Kyp";
letter1 [13] = "Gonk";
letter1 [14] = "Zlung";
letter1 [15] = "Adi";
letter1 [16] = "Nat";
letter1 [17] = "Ru";
letter1 [18] = "Cla";
letter1 [19] = "Kir";
letter1 [20] = "Obi";
letter1 [21] = "Ken";
letter1 [22] = "Ziro";
letter1 [23] = "Tion";
letter1 [24] = "Zev";
letter1 [25] = "Jar";
letter1 [26] = "Tavion";


letter2 = new String[27];//Name generation array2 uses first letter of the last name
letter2 [0] = "tzki";
letter2 [1] = "hut";
letter2 [2] = "der";
letter2 [3] = "kzos";
letter2 [4] = "vos";
letter2 [5] = "vader";
letter2 [6] = "thrawn";
letter2 [7] = "mesk";
letter2 [8] = "thuo";
letter2 [9] = "skywalker";
letter2 [10] = "D2";
letter2 [11] = "maul";
letter2 [12] = "sith";
letter2 [13] = "muzzar";
letter2 [14] = "jusik";
letter2 [15] = "horn";
letter2 [16] = "phisto";
letter2 [17] = "farlander";
letter2 [18] = "dunhaussan";
letter2 [19] = "jar";
letter2 [20] = "binks";
letter2 [21] = "lbis";
letter2 [22] = "gnarzlo";
letter2 [23] = "anakin";
letter2 [24] = "ackbur";
letter2 [25] = "axmis";
letter2 [26] = "bacca";



planet = new String[11]; //Planet options

planet[0] = "Alderaan";
planet[1] = "Bespin";
planet[2] = "Coruscant";
planet[3]= "Forest moon of Endor";
planet[4] = "Hoth";
planet[5] = "Kamino";
planet[6] = "Kashyyk";
planet[7] = "Mustafar";
planet[8] = "Yavin";
planet[9] = "DEATH STAR";//Planet options -END

rank = new String[50]; //Diferent jobs or prefixes to the name
rank[0] = "Clone";
rank[1] = "Bounty Hunter";
rank[2] = "Droid";
rank[3]= "Jedi Knight";
rank[4] = "Gungan";
rank[5] = "Gangster";
rank[6] = "commander";
rank[7] = "Queen";
rank[9] = "Empirer";
rank[10] = "Darth";
rank[11] = "captain";
rank[12] = "ewok";
*/
System.out.println("Welcome to the Star Wars name generator");
System.out.println("What is your first name?"); //Name Generation
String firstName = input.nextLine(); //Name Generation (i want to take the first letter of there input and get the output from letter1 matching that # ie c = letter1 [2] a= letter1 [0])
System.out.println("What is your last name?"); //Name Generation
String lastName = input.nextLine(); //Name Generation (i want to take the first letter of there input and get the output from letter2 matching that # ie c = letter2 [2] a= letter2 [0])


/*
System.out.println("If you had to choose dark or light side?"); //Selection of Dark or Light side
side = input1.nextString(); //Selection of Dark or Light side

if (side = dark);
{
System.out.println("You Have chosen to be part of the dark side!");

}


else
{
System.out.println("You are now part of the light side!");

}
*/

System.out.println("There are now several places you could live please choose one of the following by number:"); //Planet selections
System.out.println("1) Alderaan 2) Bespin 3) Coruscant 4) Forest moon of Endor 5) Hoth "); //Planet selections
System.out.println("6) Kamino 7) Kashyyk 8) Mustafar 9) Yavin 10)DEATHSTAR"); //Planet selections
location = input1.nestString();



//Final print out Statements
System.out.println("You "/*instert name1 and name2 */" now fight for the "/*side they choose */" As a proud "/* Job/creature */"of the"+ location +"Good luck against your enemy's and may the force be with you");

}

我收到以下错误:

静态映射firstNameMap;firstNameMap = new HashMap<>(); - 找不到符号

firstNameMap.put("A", "Cho"); - 初始化期间静态非最终变量的使用

最佳答案

首先,我建议使用 HashMap ,将第一个字母映射到名称,而不是数组。然后,根据每个输入名称的第一个字符查找名称。它看起来像

public class Starwars {
private static Map<String, String> firstNameMap = new HashMap<String, String>();
static {
firstNameMap.put("A", "Cho");
// etc.
}

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.println("Welcome to the Star Wars name generator");
System.out.println("What is your first name?");
String firstName = input.nextLine();
String newName = firstNameMap.get(firstName.toUpperCase().substring(0,1));

System.out.println("What is your last name?");
String lastName = input.nextLine();
// Do something similar
}
}

关于java - 从输入的第一个字母调用字符串输入的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26539675/

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