Java For循环,输入分数数

Java For循环,输入分数数,java,loops,for-loop,Java,Loops,For Loop,我是Java编程的初学者,我需要在程序进入for循环之前指出分数。以下是一个例子: 如果我输入'4'作为输入,它将声明输入分数4次,并获得平均值 试用代码: import java.io.*; public class Exer2{ public static void main(String[] args)throws Exception{ BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

我是Java编程的初学者,我需要在程序进入for循环之前指出分数。以下是一个例子:

如果我输入'4'作为输入,它将声明输入分数4次,并获得平均值

试用代码:

import java.io.*;
public class Exer2{
public static void main(String[] args)throws Exception{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String strinput;

int i;
int x;
x = Integer.parseInt(br.readLine());

System.out.print("Enter number or scores here:");
x = input.nextInt();

for (i = 0; i < x; i++){
    System.out.print("Enter score here:");
    if (i==0){
        strinput = input.readLine();
        x=Integer.parseInt(br.readLine()));
    }

}

i=(a+b+c)/x;
System.out.println("The average score is " +i);
import java.io.*;
公共课练习2{
公共静态void main(字符串[]args)引发异常{
BufferedReader输入=新的BufferedReader(新的InputStreamReader(System.in));
弦纹;
int i;
int x;
x=Integer.parseInt(br.readLine());
System.out.print(“在此处输入数字或分数:”);
x=input.nextInt();
对于(i=0;i
您必须阅读输入,使用

x = Integer.parseInt(input.readLine());

也许在重新命名变量(x不是很明确)之后,在阅读了您的评论之后,我能够理解这个问题

我将如何指示x作为循环次数,我不知道从何开始

要保存用户的输入,请执行以下操作:

System.out.print("Enter # of scores:");
x = Integer.parseInt(input.readLine());
然后在循环中:

int scores = 0; //This will hold the sum of the scores
    for (i = 1; i <= x; i++)
    {
        System.out.print("Enter score " + i + ": "); //Ask for new score
        scores = scores + Integer.parseInt(input.readLine()); //Add score to existing sum of previous scores
    }
    System.out.println("The average score is " + scores / x); // Gets the average score and prints to console
int scores=0;//这将保存分数之和

对于(i=1;我闻起来像是家庭作业。你的问题是什么?你的问题是什么?你可以像读取每个分数一样读取分数。我还建议你使用数组,而不是命名变量。我会说x=input.readline()是你想要的,但很难说你真正想要什么。我修正了它。我需要在循环中从1开始,而不是从0开始。