Java 试图从文本文件填充数组,但需要包含(0-100)
我制作了一个程序,从文件中获取输入(分数),并输出所有分数,以及平均分、最高分和最低分。但是,我正在努力排除不在0-100范围内的分数(排除意味着打印到监视器而不是实际输出文件),这也会影响我得到的结果。我的if/else条件似乎没有任何作用。这里是我的变量,以及我必须填充数组的循环。非常感谢您的帮助,谢谢Java 试图从文本文件填充数组,但需要包含(0-100),java,file-io,Java,File Io,我制作了一个程序,从文件中获取输入(分数),并输出所有分数,以及平均分、最高分和最低分。但是,我正在努力排除不在0-100范围内的分数(排除意味着打印到监视器而不是实际输出文件),这也会影响我得到的结果。我的if/else条件似乎没有任何作用。这里是我的变量,以及我必须填充数组的循环。非常感谢您的帮助,谢谢 int sum = 0; // sum of the grades double avg; // avg of the grades (change to mean later)
int sum = 0; // sum of the grades
double avg; // avg of the grades (change to mean later)
final int SIZE = 23; // size of the array
int[] scores = new int[SIZE]; // array to store the scores
int i = 0; // array subscript
int high = 0; // variable to hold the high grade
int low = 0; // variable to hold the low grade
// Read the input and write the output
out.printf("The scores are: ");
while (in.hasNextInt())
{
// statements to input the scores, output the scores, accumulate the
sum, and modify the subscript
if (i >= 0 && i <= 100)
{
scores[i] = in.nextInt();
sum = sum + scores[i];
out.print(scores[i]);
i++;
}
else
{
System.out.println("The ignored scores are: " + scores[i]);
}
}
int sum=0;//等级之和
双平均值;//等级平均值(以后改为平均值)
最终整数大小=23;//数组的大小
int[]分数=新的int[大小];//数组来存储分数
int i=0;//数组下标
int高=0;//变量来保持高等级
int low=0;//变量来保持低等级
//读取输入并写入输出
out.printf(“分数是:”);
while(在.hasNextInt()中)
{
//语句来输入分数、输出分数、累计
求和,并修改下标
如果(i>=0&&i您似乎将索引与输入的值混淆了
将代码更改为
while (in.hasNextInt())
{
// statements to input the scores, output the scores, accumulate the
int input = in.nextInt();
if (input >= 0 && input <= 100)
{
scores[i] = input;
sum = sum + scores[i];
out.print(scores[i]);
i++;
}
else
{
System.out.println("The ignored score is: " + input);
}
}
while(在.hasNextInt()中)
{
//语句来输入分数、输出分数、累计
int input=in.nextInt();
如果(input>=0&&input代码应该检查分数,而不是数组索引i
while (in.hasNextInt())
{
// statements to input the scores, output the scores, accumulate the
sum, and modify the subscript
int score = in.nextInt()
if (score >= 0 && score <= 100)
{
scores[i] = score;
sum = sum + scores[i];
out.print(scores[i]);
i++;
}
else
{
System.out.println("The ignored scores are: " + score);
}
}
while(在.hasNextInt()中)
{
//语句来输入分数、输出分数、累计
求和,并修改下标
int score=in.nextInt()
if(score>=0&&score-Hm,似乎仍在输出文件中打印相同的内容。不过,我会继续打印,谢谢!好的,经过一些修改后,我现在已经得到了。只是在if语句中将“I”更改为“input”