我似乎無法弄清楚如何在for循環中添加值。我應該得到的輸出是這樣的:在循環中添加值
How many numbers? 3 //user inputs numbers desired. number please 1 2 3 Total is 6
任何幫助將不勝感激!
import java.util.Scanner ;
public class AddNumbers {
public static void main(String[] args) {
int sum = 0;
int count = 1;
Scanner scan = new Scanner(System.in);
System.out.println("How many numbers?");
int n = scan.nextInt();
for(int i=0; i <= n; i++){
System.out.println ("number please");
int c = scan.nextInt();
//stuck
}
System.out.println("Total is " + sum);
}
}
只需添加'總和+ = C;'在循環 你的循環也應該是'for(int i = 0; i
iTech
2013-02-21 04:24:56
非常感謝!我對Java編碼非常缺乏經驗。 – 2013-02-21 04:33:26