Java 无法读取重复字符
我正在编写一个代码来读取一个字符串并计算重复数据集Java 无法读取重复字符,java,string,loops,char,Java,String,Loops,Char,我正在编写一个代码来读取一个字符串并计算重复数据集 public int countRepeatedCharacters() { int c = 0; for (int i = 1; i < word.length() - 1; i++) { if (word.charAt(i) == word.charAt(i + 1)) // found a repetition { if ( word.charAt(i
public int countRepeatedCharacters()
{
int c = 0;
for (int i = 1; i < word.length() - 1; i++)
{
if (word.charAt(i) == word.charAt(i + 1)) // found a repetition
{
if ( word.charAt(i - 1) != word.charAt(i)) {
c++;
}
}
}
return c;
}
public int countRepeatedCharacters()
{
int c=0;
for(int i=1;i
如果我尝试输入
aabbcdaaaabb
我应该有4组重复小数
aa | bb | aaaa | bb
我知道我没有读第一组aa,因为我的索引从1开始。我试着把它改成零,但是我试着把整个循环改成零,结果我失败了。关于如何更改索引或循环,有什么建议吗?根据我从你的问题中了解到的,你想计算重复集的数量,那么这应该会有帮助
for (int i = 0; i < word.length()-1; i++){
if (word.charAt(i) == word.charAt(i + 1)){ // found a repetition
if (i==0 || word.charAt(i - 1) != word.charAt(i)) {
c++;
}
}
}
for(int i=0;i
根据我从您的问题中了解到的情况,您想计算重复集的数量,那么这应该会有所帮助
for (int i = 0; i < word.length()-1; i++){
if (word.charAt(i) == word.charAt(i + 1)){ // found a repetition
if (i==0 || word.charAt(i - 1) != word.charAt(i)) {
c++;
}
}
}
for(int i=0;i
您可以尝试以下方法:-
public static void main(String str[]) {
String word = "aabbcdaaaabbc";
int c = 1;
for (int i = 0; i < word.length() - 1; i++) {
if (word.charAt(i) == word.charAt(i + 1)) {
c++;
} else {
System.out.println(word.charAt(i)+ " = " +c);
c = 1;
}
}
System.out.println(word.charAt(word.length()-1)+ " = " +c);
}
publicstaticvoidmain(stringstr[]){
String word=“aabbcdaaaabbc”;
int c=1;
for(int i=0;i
您可以根据需要,通过删除
系统输出
和其他内容来修改它。您可以尝试以下方法:-
public static void main(String str[]) {
String word = "aabbcdaaaabbc";
int c = 1;
for (int i = 0; i < word.length() - 1; i++) {
if (word.charAt(i) == word.charAt(i + 1)) {
c++;
} else {
System.out.println(word.charAt(i)+ " = " +c);
c = 1;
}
}
System.out.println(word.charAt(word.length()-1)+ " = " +c);
}
publicstaticvoidmain(stringstr[]){
String word=“aabbcdaaaabbc”;
int c=1;
for(int i=0;i
你可以根据你的需要修改它,通过删除<代码> SysUs和其他文件。
使用LangTh()- 1使你不考虑计算中的最后一个字符。 这将导致您丢失最后一个重复角色
最后,我会这样做: public static int countRepeatedCharacters(String word)
{
boolean withinRepeating = false;
int c = 0;
for (int i = 1; i < word.length(); i++)
{
if (!withinRepeating && (withinRepeating = word.charAt(i) == word.charAt(i - 1)))
c++;
else
withinRepeating = word.charAt(i) == word.charAt(i - 1);
}
return c;
}
public static int countRepeatedCharacters(字符串字)
{
布尔值withinRepeating=false;
int c=0;
for(int i=1;i使用LangTh()- 1导致您不考虑计算中的最后一个字符。
这将导致您丢失最后一个重复角色
最后,我会这样做:
public static int countRepeatedCharacters(String word)
{
boolean withinRepeating = false;
int c = 0;
for (int i = 1; i < word.length(); i++)
{
if (!withinRepeating && (withinRepeating = word.charAt(i) == word.charAt(i - 1)))
c++;
else
withinRepeating = word.charAt(i) == word.charAt(i - 1);
}
return c;
}
public static int countRepeatedCharacters(字符串字)
{
布尔值withinRepeating=false;
int c=0;
for(int i=1;i
尝试以下代码:
public int countRepeatedCharacters(String word)
{
int c = 0;
Character last = null;
bool counted = false;
for (int i = 0; i < word.length(); i++)
{
if (last != null && last.equals(word.charAt(i))) { // same as previous characted
if (!counted) { // if not counted this character yet, count it
c++;
counted = true;
}
}
else { // new char, so update last and reset counted to false
last = word.charAt(i);
counted = false
}
}
return c;
}
public int countRepeatedCharacters(字符串字)
{
int c=0;
字符last=null;
布尔数=假;
for(int i=0;i
编辑-aaaa计为4,固定为1,请尝试以下代码:
public int countRepeatedCharacters(String word)
{
int c = 0;
Character last = null;
bool counted = false;
for (int i = 0; i < word.length(); i++)
{
if (last != null && last.equals(word.charAt(i))) { // same as previous characted
if (!counted) { // if not counted this character yet, count it
c++;
counted = true;
}
}
else { // new char, so update last and reset counted to false
last = word.charAt(i);
counted = false
}
}
return c;
}
public int countRepeatedCharacters(字符串字)
{
int c=0;
字符last=null;
布尔数=假;
for(int i=0;i
编辑-aaaa计为4,固定为1----
public int countRepeatedCharacters()
{
int c=0,x=0;
布尔字符匹配=假;
for(int i=0;i
试试这个----
public int countRepeatedCharacters()
{
int c=0,x=0;
布尔字符匹配=假;
for(int i=0;i