如果代码中没有数字,则 return 为 true 的方法

method that will return true if there is no digit in code

 package code;
 public class WriteUp{

 public boolean containsNoDigits(String s){
    s  = s.toLowerCase();
    char[] n = {'0', '1', '2', ....., '9'} //i wrote out 0-9
    for (int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if (c == n[i]);
                  return false; //if string contain digit
    }
    return true; //if string contain NO digit
 }
 }

我想写一个方法(使用数组)来检查我的字符串是否包含数字。数字 => 假;没有数字 => 真;

我的代码未能通过 JUnit 测试

JUnit 测试:

  @Test
  public void test(){
        code.WriteUp wu = new code.WriteUp();
        boolean expected = true;
        boolean actual = wu.containsNoDigits("there is no digit")
        assertTrue("", expected ==actual);
  }

  @Test
  public void test01(){
        code.WriteUp wu = new code.WriteUp();
        boolean expected = false;
        boolean actual = wu.containsNoDigits("there are digit, 0342432")
        assertTrue("", expected ==actual);
  }

如何修复代码以使其正常工作

分号终止块,删除它

if (c == n[i]); // <-- here
    return false;

实际上是

if (c == n[i]); // <-- here
return false;

你需要类似的东西(最好有大括号)

if (c == n[i]) {
    return false;
}

if (c == n[i])
    return false;

此外,正则表达式会更有效率。喜欢

public boolean containsNoDigits(String s){
    return !s.matches("\d"); // <-- digit pattern
} 

这应该有效:

public boolean hasDigit(String x){
    for (char c : x.toCharArray())
        if (Character.isDigit(c))
            return false;
    return true;
}

您可以使用 字符数组 毫不费力地遍历字符串中的所有字符并使用函数 Character.isDigit(c) 轻松检查每个字符。这可能是最简单易读的。

希望对您有所帮助。

Java 的字符 class 包含一个静态方法,用于检查字符是否为数字。 Character.isDigit(char ch) 会为您做这件事。

您可以使用此方法编写代码

public boolean hasNoDigits(String s){
   for(int i=0;i<s.length();i++){
       if(Character.isDigit(s.charAt(i))){
           return false;
       }
   }
   return true;
}

如果您只想在字符串包含标准拉丁数字 0 through 9\u0030\u0039)时使方法 return false,则可以修改您的代码是这样的:

public boolean hasNoDigits(String s){
   for(int i=0;i<s.length();i++){
       char atIndex=s.charAt(i);
       if(atIndex >= '0' && atIndex <= '9'){
           return false;
       }
   }
   return true;
}

此更改的原因是 Character.isDigit(char ch) return 所有被 Unicode class 化为数字的字符都是正确的。

Some Unicode character ranges that contain digits:

  • '\u0030' through '\u0039', ISO-LATIN-1 digits ('0' through '9')
  • '\u0660' through '\u0669', Arabic-Indic digits
  • '\u06F0' through '\u06F9', Extended Arabic-Indic digits
  • '\u0966' through '\u096F', Devanagari digits
  • '\uFF10' through '\uFF19', Fullwidth digits

这意味着包含此 或此 的字符串将 return 为真。

再次仔细阅读我的代码后,我知道了为什么我搞砸了并找到了解决方案。

   public boolean containsNoDigits(String s) {
    s = s.toLowerCase();
    char[] n = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (c == n[0] || c == n[1] || c == n[2] || c == n[3] || c == n[4] || 
                c == n[5] || c == n[6] || c == n[7] ||  c == n[8] || c == n[9]) {
            return false; // if s has no digit
        }
    }
    return true; // if s has digit
}

感谢您提供更短、更高效的解决方案。我是初学者,因此我尽可能地编写我的代码,这个解决方案是为了让我更好地理解数组的使用。