可以优化不,你的代码长什么样?
public static void game2(){
//游戏2:让用户输入两个数字,你来计算出这两个数字间的所有整数和
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个整数:");
int m = sc.nextInt();
int n = sc.nextInt();
int sum = 0;
if (m>n) {
int temp = m;
m = n;
n = temp;
}else if (m==n) {
System.out.println(0);
}else {
int x = m+1;
while (x<n) {
sum += x;
x++;
}
System.out.println(sum);
}
} |