我是Java新手,非常感谢任何人都能给我的帮助
我需要编写一个程序,要求用户输入一个包含5个字符的字符串,例如asdfg,然后将它们转换为单词,例如'apple spinach dog fish goat'。 (这些是用户输入的字母)
我一直在尝试将这些输入到数组中,并从中更改元素,但我不完全确定如何。
这是我到目前为止的代码
<section class="container portfolio">
<ul>
<li><img src="http://lantosistvan.com/temp/freecodecamp/IMG_2959-1400px.jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/lantosistvan-portfolio-010.jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/IMG_6188-dng-k.jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/20151220-csaladi-peregi-046-k.jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/20151230-csalad-szalai-0194-k.jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/lantosistvan-portfolio-001(1).jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/160528171819-portfolio.jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/160528171829-portfolio.jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/160528171938-portfolio.jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/160528171953-portfolio.jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/160528194754-portfolio.jpg" alt="" /></li>
<li><img src="http://lantosistvan.com/temp/freecodecamp/160528184948-portfolio.jpg" alt="" /></li>
</ul>
</section>
感谢任何人的帮助!
答案 0 :(得分:2)
将所有这些东西放在地图中会更好。
Map<Character,String> dataMap = new HashMap<Character,String>();
dataMap.put('a',"apple");
dataMap.put('s',"spinach");
dataMap.put('d',"dog");
dataMap.put('f',"fish");
dataMap.put('g',"goat");
现在按所需顺序打印数据,如下所示
//Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(dataMap.get(myList.charAt(i)) + " ");
}
答案 1 :(得分:0)
现在代码存在一些问题。首先,您需要将收到的字符串转换为字符数组。然后,要使用列表实现,您必须遍历两个列表,以使每个输入字符与以该字母开头的单词匹配。
更好的选择是将字母配对放在地图中并以这种方式检索它们。
最后,您可能不想使用&#34; System.out.println&#34;,因为这样您就可以在不同的行上获得结果。相反,使用&#34; System.out.print。&#34;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the details of your order");
String myString = scan.nextLine();
char[] myChars = myString.toCharArray();
Map<Character, String> map = new HashMap<>();
map.put('a', "apple");
map.put('s', "spinache");
map.put('d', "dog");
map.put('f', "fish");
map.put('g', "goat");
//Print all the array elements
for (int i = 0; i < myChars.length; i++) {
System.out.print(map.get(myChars[i]) + " ");
}
}
答案 2 :(得分:0)
问题不是很清楚。但这是解决问题的简单方法
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the details of your order");
String myList = scan.nextLine();
for(int i=0;i<myList.length();i++){
switch(myList.charAt(i)){
case 'a':System.out.println("Apple");
break;
case 's':System.out.println("Spinach");
break;
case 'd':System.out.println("Dog");
break;
case 'f':System.out.println("Fish");
break;
case 'g':System.out.println("Goat");
break;
default: System.out.println("Nothing..");
break;
}
}
}
答案 3 :(得分:0)
试试这个:
['/Users/austinjohnson/Desktop/streakscraper', '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
Traceback (most recent call last):
File "/Users/austinjohnson/Desktop/streakscraper/login.py", line 6, in <module>
from selenium import webdriver
ImportError: cannot import name 'webdriver'
[Finished in 0.1s with exit code 1]
[cmd: ['/usr/local/bin/python3', '-u', '/Users/austinjohnson/Desktop/streakscraper/login.py']]
[dir: /Users/austinjohnson/Desktop/streakscraper]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
我认为使用地图解决这个问题可能更合适。
String inputString = "asdfg"; //your scan.next();
//use array constructor
String [] myList = {"apple", "spinach", "dog", "fish", "goat"};
//start our result string as empty
String result = "";
//for each letter of our input string...
for(char letter: inputString.toCharArray()){
//...check each of the words in my list to see if they start with that letter
for(String word: myList){
if(word.toCharArray()[0] == letter){
result = result + " " + word;
}
}
}
System.out.println(result); //prints "apple spinach dog fish goat"
然后你也可以使用reduce来实现这个功能。
//build a map
String inputString = "asdfg"; //your scan.next();
Map<String, String> myMap = new HashMap<>();
myMap.put("a", "apple");
myMap.put("s", "spinach");
myMap.put("d", "dog");
myMap.put("f", "fish");
myMap.put("g", "goat");
//initilize our result string
String result = "";
//split the string and get our values straight from the map
for(String letter: inputString.split("")){
result = result + " " + myMap.get(letter);
}
System.out.println(result); //" apple spinach dog fish goat"