【发布时间】:2017-05-25 01:57:57
【问题描述】:
import java.util.*;
class VowelAsc
{
public static void main(String args[])
{
int count=0;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String [] s=new String[n];
int [] b=new int[40];
for(int i=0;i<n;i++)
{
s[i]=sc.next();
}
for(int i=0;i<s.length;i++)
{
char[] a=s[i].toCharArray();
for(int c=0;c<a.length;c++)
{
if(a[c]=='a' || a[c]=='e' || a[c]=='i' || a[c]=='o' || a[c]=='u' ||a[c]=='A' ||a[c]=='E' || a[c]=='I' || a[c]=='O' || a[c]=='U')
{
count++;
//b[c]=count;
}
}
if(count>0)
{
if(i<s.length)
{
String t=s[i];
s[i]=s[i+1];
s[i+1]=t;
}
}
}
}
}
我正在尝试计算每个字符串中存在的元音,并且我想根据我无法做到的计数变量交换字符串。 接受字符串后,我使用 toCharArray() 函数将其转换为 char 数组,并将每个字符与大小写元音进行比较。
我收到一个错误。在编写代码部分的任何帮助将不胜感激。
输入:
n=4
xyz
bad
aeiou
hello
输出:
aeiou
hello
bad
xyz
【问题讨论】:
-
你的问题/问题是?
-
错误是什么?
-
“我收到一个错误” - 哪个错误?
-
如何根据每个字符串中存在的元音数量以升序交换字符串数组中的字符串。
-
"ArrayIndexOutOfBoundsException" 可能是由 "s[i+1]" 当 i=(s.length-1) 引起的。
标签: java string sorting indexoutofboundsexception