[英]Trying to return true if all the letters in a string are the same
What I have so far:到目前为止我所拥有的:
public boolean allSameLetter(String str)
{
for (int i = 1; i < str.length(); i++)
{
int charb4 = i--;
if ( str.charAt(i) != str.charAt(charb4))
{
return false;
}
if ( i == str.length())
{
return true;
}
}
}
Please excuse any inefficiencies if any;如果有任何效率低下,请原谅; still relatively new to coding in general.总的来说,对于编码来说还是相对较新的。 Am I lacking some knowledge in terms of using operators and .charAt() together?我在使用运算符和 .charAt() 方面缺乏一些知识吗? Is it illogical?这不合逻辑吗? Or is my error elsewhere?还是我的错误在其他地方?
Using regex:使用正则表达式:
return str.matches("^(.)\\1*$");
Using streams:使用流:
str.chars().allMatch(c -> c == str.charAt(0));
Other:其他:
return str.replace(String.valueOf(str.charAt(0), "").length() == 0;
You can follow the below steps:您可以按照以下步骤操作:
(1) Get the first character (ie, 0th index) (1) 获取第一个字符(即第0个索引)
(2) Check the first character is the same with subsequent characters, if not return false
(and comes out from method) (2) 检查第一个字符是否与后续字符相同,如果不相同则返回false
(并从方法中出来)
(3) If all chars match ie, processing goes till the end of the method and returns true
(3) 如果所有字符都匹配ie,则处理直到方法结束并返回true
public boolean allSameLetter(String str) {
char c1 = str.charAt(0);
for(int i=1;i<str.length;i++) {
char temp = str.charAt(i);
if(c1 != temp) {
//if chars does NOT match,
//just return false from here itself,
//there is no need to verify other chars
return false;
}
}
//As it did NOT return from above if (inside for)
//it means, all chars matched, so return true
return true;
}
As Andrew said, you are decreasing i
within your for loop.正如安德鲁所说,你在 for 循环中减少i
。 You can fix this by changing it to int charb4 = i - 1;
您可以通过将其更改为int charb4 = i - 1;
来解决此问题int charb4 = i - 1;
. . As for making your code more efficient you could condense it down to this.至于使您的代码更高效,您可以将其浓缩为这一点。
public boolean allSameLetter(String str) {
for(char c : str.toCharArray())
if(c != str.charAt(0)) return false;
return true;
}
Comment if you don't understand a part of it :)如果您不理解其中的一部分,请发表评论:)
public boolean allSameLetter(String str)
{
for (int i = 1; i < str.length() -1; i++)
{
if ( str.charAt(i) != str.charAt(i+1))
{
return false;
}
}
return true
}
-1 is there since I am checking the current value in the array, then the next value in the array, thus I need to stop a place earlier. -1 是因为我正在检查数组中的当前值,然后是数组中的下一个值,因此我需要提前停止一个地方。
If the loop if statement is never entered, it will make it far enough into the code to return true如果从不输入循环 if 语句,它将使其进入代码足够远以返回 true
You have to create a for loop that searches through the length of the String - 1. this way the program will not crash because of a 3 letter word with the program trying to get the 4th letter.您必须创建一个 for 循环来搜索字符串的长度 - 1。这样程序就不会因为 3 个字母的单词而导致程序尝试获取第 4 个字母而崩溃。 This is what works for me:这对我有用:
public boolean allSameLetter(String str)
{
for(int i = 0; i< str.length()-1; i++){
if (str.charAt(i) != str.charAt(i+1)){
return false;
}
}
return true;
}
if((new HashSet<Character>(Arrays.asList(s.toCharArray()))).size()==1) return true; return false;
这应该足够了
The bug is caused by该错误是由
int charb4 = i--;
this line is equal to这条线等于
int charb4 = i-1;
i=i-1;
Because of this, your loop will never stop.因此,您的循环将永远不会停止。
The easiest way to fix this解决这个问题的最简单方法
public boolean allSameLetter(String str)
{
for (int i = 1; i < str.length(); i++)
{
if ( str.charAt(i) != str.charAt(i-1))
{
return false;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.