java for循环输入
时间: 2024-01-23 09:11:44 浏览: 93
Java for循环
Java中使用for循环来输入数字的方法如下所示:
```java
import java.util.Scanner;
public class ForLoopInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数字的个数:");
int count = scanner.nextInt();
double sum = 0;
double average = 0;
for (int i = 1; i <= count; i++) {
System.out.println("请输入第" + i + "个数字:"); double number = scanner.nextDouble();
sum += number;
average = sum / i;
System.out.println("当前求和结果为:" + sum);
System.out.println("当前平均数结果为:" + average);
}
System.out.println("结束计算,总和结果为:" + sum);
System.out.println("结束计算,平均数结果为:" + average);
scanner.close();
}
}
```
阅读全文