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

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

How to check if a char is upper/lowercase?

问题

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

  1. static int strScore(String str[], String s, int n) {
  2. int score = 0, index = 0;
  3. for (int i = 0; i < n; i++) {
  4. if (str[i].equals(s)) {
  5. for (int j = 0; j < s.length(); j++)
  6. score += s.charAt(j) - 'a' + 1;
  7. index = i + 1;
  8. break;
  9. }
  10. }
  11. score = score * index;
  12. return score;
  13. }
  14. public static void main(String[] args) {
  15. String str[] = { "abcde" };
  16. String s = "abcde";
  17. int n = str.length;
  18. int score = strScore(str, s, n);
  19. System.out.println(score);
  20. }
英文:

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.

  1. static int strScore(String str[], String s, int n) {
  2. int score = 0, index=0;
  3. for (int i = 0; i &lt; n; i++) {
  4. if (str[i] == s) {
  5. for (int j = 0; j &lt; s.length(); j++)
  6. score += s.charAt(j) - &#39;a&#39; + 1;
  7. index = i + 1;
  8. break;
  9. }
  10. }
  11. score = score * index;
  12. return score;
  13. }
  14. public static void main(String[] args) {
  15. String str[] = { &quot;abcde&quot; };
  16. String s = &quot;abcde&quot;;
  17. int n = str.length;
  18. int score = strScore(str, s, n);
  19. System.out.println( score);
  20. }
  21. </details>
  22. # 答案1
  23. **得分**: 4
  24. 使用`Character.isLowerCase(...)`
  25. 因此,您的`strScore`方法应该如下所示:
  26. ```java
  27. static int strScore(String str[], String s, int n) {
  28. int score = 0, index = 0;
  29. for (int i = 0; i < n; i++) {
  30. if (str[i].equals(s)) {
  31. for (int j = 0; j < s.length(); j++) {
  32. char c = s.charAt(j);
  33. if(Character.isLowerCase(c)) // <-- 这部分很重要
  34. score += c - 'a' + 1;
  35. }
  36. index = i + 1;
  37. break;
  38. }
  39. }
  40. score = score * index;
  41. return score;
  42. }

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

  1. static int strScore(String s) {
  2. int score = 0;
  3. for (int i = 0; i < s.length(); i++) {
  4. char c = s.charAt(i);
  5. if(Character.isLowerCase(c))
  6. score += c - 'a' + 1;
  7. }
  8. return score;
  9. }
英文:

Use Character.isLowerCase(...).

So this is what your strScore method should look like:

  1. static int strScore(String str[], String s, int n) {
  2. int score = 0, index = 0;
  3. for (int i = 0; i &lt; n; i++) {
  4. if (str[i].equals(s)) {
  5. for (int j = 0; j &lt; s.length(); j++) {
  6. char c = s.charAt(j);
  7. if(Character.isLowerCase(c)) // &lt;-- This is the important part
  8. score += c - &#39;a&#39; + 1;
  9. }
  10. index = i + 1;
  11. break;
  12. }
  13. }
  14. score = score * index;
  15. return score;
  16. }

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

  1. static int strScore(String s) {
  2. int score = 0;
  3. for (int i = 0; i &lt; s.length(); i++) {
  4. char c = s.charAt(i);
  5. if(Character.isLowerCase(c))
  6. score += c - &#39;a&#39; + 1;
  7. }
  8. return score;
  9. }

答案2

得分: 2

有两个问题需要解决:

  1. 您使用 == 来比较字符串。您需要使用 .equals
  2. 您需要像这样进行检查 if(s.charAt(j)>= 'a' && s.charAt(j)<'z')
  1. for (int i = 0; i < n; i++) {
  2. if (str[i].equals(s)) {
  3. for (int j = 0; j < s.length(); j++)
  4. if(s.charAt(j)>= 'a' && s.charAt(j)<'z') {
  5. 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;)
  1. for (int i = 0; i &lt; n; i++) {
  2. if (str[i].equals(s)) {
  3. for (int j = 0; j &lt; s.length(); j++)
  4. if(s.charAt(j)&gt;= &#39;a&#39; &amp;&amp; s.charAt(j)&lt;&#39;z&#39;) {
  5. 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():

  1. static int strScore(String s) {
  2. int score = 0, index = 0;
  3. for (int i = 0; i < s.length(); i++) {
  4. for (char c : s.toCharArray()) {
  5. if(c >= 'a' && c < 'z') { //alternatively if(Character.isLowerCase(c))
  6. score += c - 'a' + 1;
  7. }
  8. }
  9. index = i + 1;
  10. break;
  11. }
  12. score = score * index;
  13. return score;
  14. }
英文:

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():

  1. static int strScore(String s) {
  2. int score = 0, index = 0;
  3. for (int i = 0; i &lt; s.length(); i++) {
  4. for (char c : s.toCharArray()) {
  5. if(c &gt;= &#39;a&#39; &amp;&amp; c &lt;&#39;z&#39;) { //alternatively if(Character.isLowerCase(c))
  6. score += c - &#39;a&#39; + 1;
  7. }
  8. }
  9. index = i + 1;
  10. break;
  11. }
  12. score = score * index;
  13. return score;
  14. }

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:

确定