我想找到一種方法來從用戶輸入的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