永发信息网

问: 10 java编程:如果我们将英文字母编号A=1,B=2,…Z=26,将某个单词的各别字母

答案:2  悬赏:60  手机版
解决时间 2021-01-24 03:36
  • 提问者网友:斑駁影
  • 2021-01-23 05:36
问: 10 java编程:如果我们将英文字母编号A=1,B=2,…Z=26,将某个单词的各别字母转换成前述的数字后相加便得到一个分数。大小写A都是1,例如:Knowledge 96 Attitude 100 大小写用户输入不限,A-Z或a-z。 如果字串不是这二十四个字母的大小写就输出Fail,例如:~True:输出Fail,java编程,谢谢
最佳答案
  • 五星知识达人网友:话散在刀尖上
  • 2021-01-23 06:54
import java.util.Scanner;


public class CountWords {
private static Scanner input = new Scanner(System.in);


public static boolean jugde(String str) {
String regex = "^[a-zA-Z]{1,}$";
return str.matches(regex);
}


public static String getInputString(String tips) {
System.out.print(tips);
return input.next();
}


public static long countTheWords(String words) {
long sum = 0;
// A:65 Z:90 a:97 z:122
for (int index = 0; null != words && index < words.length(); index++) {
int wordIndex = (int) words.charAt(index);
int sub = 0;
if (wordIndex >= 65 && wordIndex <= 90) {
sub = 65;
} else if (wordIndex >= 97 && wordIndex <= 122) {
sub = 97;
}
sum += wordIndex - sub + 1;
}

return sum;
}

public static void main(String[] args) {
String str = "";
do {
str = getInputString("Input your str: ");
} while (!jugde(str));

System.out.println(countTheWords(str));
}

}
全部回答
  • 1楼网友:像个废品
  • 2021-01-23 07:14
public static void main(string[] args) { string s = &quot;attitude&quot;; int sum = 0; s = s.touppercase();  //全部转换成大写 for (int i = 0; i &lt; s.length(); i++) { char c = s.charat(i); sum += (int)c -64; } system.out.println(sum); }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯