题目描述
输入一个整数n,然后输入n个字符串,将这n个字符串保存在数组中。寻找上述字符串中长度最长并且以b开头的字符串,在一个独立行中输出这个字符串在数组中的下标、字符串本身的值,中间用空格分开。如果有多个满足要求的元素,只需输出其中下标最小的那个元素的相关内容。如果没有以b开头的字符串,则输出-1。
输入样例
5
wonderful beauty peace bit am
输出样例
1 beauty
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String target = "";
int index = -1;
System.out.println("请输入目标语句长度");
String length = sc.nextLine();
String[] item = new String[new Integer(length)];
System.out.println("请输入目标语句");
String sentence = sc.nextLine();
item = sentence.split(" ");
for (int i = 0; i < item.length; i++) {
if (item[i].length()>target.length()&&item[i].startsWith("b")) {
target = item[i];
index = i;
}
}
if (index == -1){
System.out.println(index);
}else {
System.out.println(index + " " +target);
}
}