所以我工作的这个问题,在用户输入X的个数和结果是这样计算出0和X之间的所有数字的总和:7和倍数添加数字减去倍数
- 倍数11个不包括在总和中。既7和11的
- 但倍数被包括为前:77
例如:
用户输入X >> 80
总和>>(0 + 1 + 2 + 3 + ... 77)-(7,11,14,21,22 ... ... 77)+(77)=导致
import java.util.Scanner;
public class testadd {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Please the number greater than 77");
// int a = 0;
int b = keyboard.nextInt();
int sum = 0;
int s = Math.min(0, b);
int e = Math.max(0, b);
System.out.println(s);
System.out.println(e);
int x=0;
for(int i=1; i<e; i++){
if(i%7 ==0 || i%11 ==0){
x=x+i;
System.out.println(x + " values of x");
}
}
while (s <= e) {
sum += s;
s++;
}
System.out.print("The sum of the numbers between " + 0 + " and " + b + " is " + sum);
}
}
为什么你使用'Math.min'和'Math.max' ?如果'b'大于77,那么'Math.min(0,b)'总是0,所以没有理由使用'Math.min'使事情复杂化。类似的'Math.max',但我会让你弄清楚'e'会被设置为什么。 – ajb