我已经在这个计划上打了一堵砖墙
public class Assignment11 {
public int sumConstants ()
{
char ch = 'A';
for (int i = 0 ; i<=90 && i >= 65 ; i++)
{
if (Character.isUpperCase(ch))
{
int x = (int) ch;
}
}
return ch;
}
public static void main(String[] args) {
Scanner in = new Scanner ( System.in );
Assignment11 a11 = new Assignment11();
int x = a11.sumConstants();
System.out.println("Enter TWO upper case Characters:");
String s = in.nextLine();
System.out.println("The sum of the constants is: " + x);
}
}
这是我最终的输出
Enter TWO upper case Characters:
AE
The sum of the constants is: 65
我想要的输出示例是:
Enter TWO upper case Characters:
IO
The sum of the constants is: 380
所以基本上发生的是&#34; JKLMN&#34;的ASCII值。被加在一起。
答案 0 :(得分:0)
为您的方法添加2个参数,并将2个字符传递给方法:
public static int sumConsonants(char start, char end){
int sum = 0;
for(int x=start+1; x<end; x++){
if(!Character.toString((char)x).matches("[aeiouAeiou]+")) //if is consonant
sum += x; //add to sum
}
return sum;
}
完成运行
Scanner scn = new Scanner(System.in);
char start = scn.nextLine().charAt(0);
char end = scn.nextLine().charAt(0);
System.out.println("The sum of the constants is: " + sumConsonants(start, end));
<强>测试强>
System.out.pritln(sumConsonants('A', 'E')); //66 + 67 + 68
<强>输出:强>
201