如何检查字符是大写还是小写?

huangapple go评论91阅读模式
英文:

How to check if a char is upper/lowercase?

问题

以下是翻译好的代码部分:

static int strScore(String str[], String s, int n) { 
    int score = 0, index = 0; 

    for (int i = 0; i < n; i++) { 
        if (str[i].equals(s)) { 
            for (int j = 0; j < s.length(); j++) 
                score += s.charAt(j) - 'a' + 1; 
            index = i + 1; 
            break; 
        } 
    } 
      
    score = score * index; 
    return score; 
} 
      
public static void main(String[] args) {        
    String str[] = { "abcde" };
    String s = "abcde"; 
    
    int n = str.length; 
    int score = strScore(str, s, n); 
    System.out.println(score); 
}
英文:

The following code is supposed to convert letters to numbers and give the sum, but ignore any letters that are uppercase.

Example:

The input abcde should return 15. The input abCde should return 12.

Any help is appreciated.

static int strScore(String str[], String s, int n) { 
    int score = 0, index=0; 

    for (int i = 0; i &lt; n; i++) { 
    	if (str[i] == s) { 

    	for (int j = 0; j &lt; s.length(); j++) 
    	    score += s.charAt(j) - &#39;a&#39; + 1; 
    	    index = i + 1; 
    	    break; 
    	} 
    } 
    	  
    score = score * index; 
    return score; 
} 
    	  	
public static void main(String[] args) {		
    String str[] = { &quot;abcde&quot; };
    String s = &quot;abcde&quot;; 
    
    int n = str.length; 
    int score = strScore(str, s, n); 
    System.out.println( score); 
}

</details>


# 答案1
**得分**: 4

使用`Character.isLowerCase(...)`。

因此,您的`strScore`方法应该如下所示:

```java
static int strScore(String str[], String s, int n) { 
    int score = 0, index = 0; 
    for (int i = 0; i < n; i++) { 
        if (str[i].equals(s)) { 
            for (int j = 0; j < s.length(); j++) {
                char c = s.charAt(j);
                if(Character.isLowerCase(c)) // <-- 这部分很重要
                    score += c - 'a' + 1;
            }
            index = i + 1; 
            break; 
        } 
    }

    score = score * index; 
    return score; 
}

正如评论中指出的,不需要str参数,因此也不需要n参数。以下是更好的版本:

static int strScore(String s) { 
    int score = 0;

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if(Character.isLowerCase(c))
            score += c - 'a' + 1;
    }

    return score; 
}
英文:

Use Character.isLowerCase(...).

So this is what your strScore method should look like:

static int strScore(String str[], String s, int n) { 
    int score = 0, index = 0; 
    for (int i = 0; i &lt; n; i++) { 
        if (str[i].equals(s)) { 
            for (int j = 0; j &lt; s.length(); j++) {
                char c = s.charAt(j);
                if(Character.isLowerCase(c)) // &lt;-- This is the important part
                    score += c - &#39;a&#39; + 1;
            }
            index = i + 1; 
            break; 
        } 
    }

    score = score * index; 
    return score; 
}

As pointed out in the comments, there is no need for the str and therfore neither the n parameter. This is a better version:

static int strScore(String s) { 
    int score = 0;

    for (int i = 0; i &lt; s.length(); i++) {
        char c = s.charAt(i);
        if(Character.isLowerCase(c))
            score += c - &#39;a&#39; + 1;
    }

    return score; 
}

答案2

得分: 2

有两个问题需要解决:

  1. 您使用 == 来比较字符串。您需要使用 .equals
  2. 您需要像这样进行检查 if(s.charAt(j)>= 'a' && s.charAt(j)<'z')
for (int i = 0; i < n; i++) { 
    if (str[i].equals(s)) { 
	    for (int j = 0; j < s.length(); j++) 
	    	if(s.charAt(j)>= 'a' && s.charAt(j)<'z') {
	            score += s.charAt(j) - 'a' + 1; 
英文:

There are two things to address:

  1. You have used == to compare strings. You need to use .equals
  2. You need to put a check like if(s.charAt(j)&gt;= &#39;a&#39; &amp;&amp; s.charAt(j)&lt;&#39;z&#39;)
for (int i = 0; i &lt; n; i++) { 
    if (str[i].equals(s)) { 
	    for (int j = 0; j &lt; s.length(); j++) 
	    	if(s.charAt(j)&gt;= &#39;a&#39; &amp;&amp; s.charAt(j)&lt;&#39;z&#39;) {
	            score += s.charAt(j) - &#39;a&#39; + 1; 

答案3

得分: 1

You can avoid passing String str[] = { "abcde" }; which has one element which equals s to The method. You can also avoid passing n which is an simply str.length():

static int strScore(String s) {
	int score = 0, index = 0;
	for (int i = 0; i < s.length(); i++) {
		for (char c : s.toCharArray()) { 
			if(c >= 'a' && c < 'z') {     //alternatively if(Character.isLowerCase(c))
				score += c - 'a' + 1;
			}
		}
		index = i + 1;
		break;
	}
	score = score * index;
	return score;
}
英文:

You can avoid passing String str[] = { &quot;abcde&quot; }; which has one element which equals s
to The method. You can also avoid passing n which is an simply str.length():

static int strScore(String s) {
	int score = 0, index = 0;
	for (int i = 0; i &lt; s.length(); i++) {
		for (char c : s.toCharArray()) { 
			if(c &gt;= &#39;a&#39; &amp;&amp; c &lt;&#39;z&#39;) {     //alternatively if(Character.isLowerCase(c))
				score += c - &#39;a&#39; + 1;
			}
		}
		index = i + 1;
		break;
	}
	score = score * index;
	return score;
}

huangapple
  • 本文由 发表于 2020年4月4日 23:40:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61030562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定