我想找到一种方法来从用户输入的int倒数,然后添加每个第二个值,因为我计数降至0for循环递减2到0,并总结得到0之前生成的值
例如。
{用户输入10
程序递减计数8,6,4,2,0
然后加入10 + 8 + 6 + 4 + 2 + 0 = 30
}
我怎样才能做到这一点使用嵌套for循环
到目前为止我只能够采取用户输入,并每次倒数2。我达到0,但无法添加每秒的值。 我的代码:
到目前为止,它只是计数为0
公共类Week5b { 静态扫描仪userVal =新的扫描仪(System.in);
public static void main(String[] args) {
//printTable();
reverseAddSkip();
public static void reverseAddSkip(){
System.out.println("Please enter an integer");
for (int i = userVal.nextInt(); i >=0; i-=2){
System.out.println(i) ;
}/* this creates a loop where the variable i is equal to user input;
the condition for the loop to continue is whether the input is larger or equal to 0; the update part of the loop takes 2 away each time, as if it were -- (which takes away one each time) */
}
} 我怎么会写说出来数学? 将i- = 2的总和加到i的原始值上。 你键入11,它计数9 7 5 3 1,然后添加11 9 7 5 3 1,并给你总和。
不知道如何将每2个数字从用户值中减去2。
您输入把50,它倒计时以2比0 你把它51倒计时,以2比0 ,但我还没有找到离开总结得到0 之前生成的所有然后是数字:/
你如何申报Java中的int变量?你如何在Java中添加两个数字? – 2014-11-08 15:47:31