System.out.println("Enter a sentence to get it translated into Pig Latin: ");
String sentence=kb.nextLine();
String[] words = sentence.split(" ");
char a=words.charAt(0);
答案 0 :(得分:1)
words
是一个数组,charAt()
方法是为String
定义的。所以你想使用:
words[index].charAt(0)
相反,index
是数组中String
的位置。
答案 1 :(得分:0)
单词是一个字符串数组。尝试使用单词[index]。分割后,子字符串将存储如下 -
Enter = words[0]
a = words[1]
sentence = words[2]
to = words[3]
get = words[4]
it = words[5]
translated = words[6]
into = words[7]
Pig = words[8]
Latin: = words[9]
访问单词[index] .chartAt(0)将返回相应子字符串的第一个字符。