为什么我的程序 return 不是正确的数字?
Why doesn't my program return the correct number?
import java.util.Scanner;
public class AnalyzingScores
{
public static void main(String[] args)
{
int count = 0;
double scoreTotal = 0;
int index;
int tests;
double scoreAverage;
double highest;
double lowest;
//Creates a new Scanner.
Scanner keyboard = new Scanner(System.in);
//Asks the user to enter how many tests they have taken.
System.out.println("Enter the amount of tests you have taken: ");
tests = keyboard.nextInt();
//Creates an array.
int[] score = new int[tests];
//Creates a for loop that asks a user to enter their test scores.
for (index = 0; index < tests; index++)
{
System.out.println("Enter your test score: ");
score[index] = keyboard.nextInt();
scoreTotal += score[index];
}
scoreAverage = scoreTotal / score.length;
System.out.println("You entered " + tests + " scores.");
System.out.println("The test average is " + scoreAverage);
System.out.println("Number of scores above or equal to the average is " + getHighest(score));
System.out.println("Number of scores below the average is " + getLowest(score));
}
private static int scoreAverage;
public static int getHighest(int[] score)
{
int aboveAverage = 0;
for (int index = 1; index < score.length; index++)
{
if (score[index] >= scoreAverage)
{
aboveAverage++;
}
}
return aboveAverage;
}
public static int getLowest(int[] score)
{
int belowAverage = 0;
for (int index = 1; index < score.length; index++)
{
if (score[index] < scoreAverage)
{
belowAverage++;
}
}
return belowAverage;
}
}
大家好!现在我的代码有问题,我不知道为什么。该代码应该发回高于或等于平均分和低于平均分的分数。
不过,有时代码不会 return 正确的数字。例如,如果我输入 3 个考试成绩,分别是 100、90 和 80,平均分是 90。它应该显示 2 个是 above/equal 到 90,1 个低于 90。问题是,它显示0 低于 90。我已经对其进行了多次测试,它似乎只发生在低于平均水平。提前致谢!
在 getHighest(int[] score)
和 getLowest(int[] score)
方法中,数组的索引应该从 0 开始,而不是 1。这样您就错过了输入的第一个值。
您没有检查getHeighest()
和getLowest()
中第0
个索引中的元素。因此,您得到的 比 aboveAverage
或 belowAverage
.
中预期的值少
删除行
private static int scoreAverage;
并将 scoreAverage
传递给两个函数。
代码:
public static int getHighest(int[] score, double scoreAverage)
{
int aboveAverage = 0;
//Index should start from 0 to avoid skipping the first element of the array.
for (int index = 0; index < score.length; index++)
{
if (score[index] >= scoreAverage)
{
aboveAverage++;
}
}
return aboveAverage;
}
public static int getLowest(int[] score, double scoreAverage)
{
int belowAverage = 0;
//Here also, index should start from 0.
for (int index = 0; index < score.length; index++)
{
if (score[index] < scoreAverage)
{
belowAverage++;
}
}
return belowAverage;
}
}
for (int index = 1; index < score.length; index++)
Java(以及大多数编程语言)中的数组使用 0-based numbering。你的循环应该从 int index = 0
开始,而不是 int index = 1
.
其他人已经回答说问题出在你的 for 循环中的索引应该是基于 0 的,这肯定是你代码中的一个错误。然而,不是问题所在。
在函数 getHighest()
和 getLowest()
中,变量 scoreAverage
指的是在定义函数之前声明的未初始化 class 级变量,即:
private static int scoreAverage;
不是函数 main()
中声明的 scoreAverage
。 class 级别变量默认为 0,这就是为什么您看不到任何低于平均值的值。
您可以通过删除 main()
中的 scoreAverage
声明并将 class 级别声明修改为:
来修复您的代码
private static double scoreAverage;
它必须是双精度数,而不是整数,因为除法 returns 是浮点类型,而不是整数。
或者,您可以使用在 main()
中声明的 double 类型的变量作为平均得分,并将其传递给两个函数,而不是访问 class 级别变量,这可能是更可取。
正如其他人所提到的,您的 for 循环应该从索引 0 开始:
for (int index = 0; index < scrore.length; index++)
但是您的代码还有另一个问题,您声明了两个名为 scoreAverage 的变量:一个在 main 方法中:
double scoreAverage;
还有一次在主要方法下面作为静态字段:
private static int scoreAverage
;
因此您只需要做一项更改:
private static double scoreAverage;
public static void main(String[] args)
{
int count = 0;
double scoreTotal = 0;
int index;
int tests;
//double scoreAverage; <--- remove that
double highest;
double lowest; ...
你的代码应该可以工作。
您调用的方法:getHighest() 和 getLowest() 没有正确的 scoreAverage 变量。在该方法的代码中,scoreAverage 变量等于 0。因此您可以这样做:
public static int getHighest(int[] score, double scoreAverage)
{
int aboveAverage = 0;
for (int index = 0; index < score.length; index++)
{
if (score[index] >= scoreAverage)
{
aboveAverage++;
}
}
return aboveAverage;
}
public static int getLowest(int[] score, double scoreAverage)
{
int belowAverage = 0;
for (int index = 0; index < score.length; index++)
{
if (score[index] < scoreAverage)
{
belowAverage++;
}
}
return belowAverage;
}
import java.util.Scanner;
public class AnalyzingScores
{
public static void main(String[] args)
{
int count = 0;
double scoreTotal = 0;
int index;
int tests;
double scoreAverage;
double highest;
double lowest;
//Creates a new Scanner.
Scanner keyboard = new Scanner(System.in);
//Asks the user to enter how many tests they have taken.
System.out.println("Enter the amount of tests you have taken: ");
tests = keyboard.nextInt();
//Creates an array.
int[] score = new int[tests];
//Creates a for loop that asks a user to enter their test scores.
for (index = 0; index < tests; index++)
{
System.out.println("Enter your test score: ");
score[index] = keyboard.nextInt();
scoreTotal += score[index];
}
scoreAverage = scoreTotal / score.length;
System.out.println("You entered " + tests + " scores.");
System.out.println("The test average is " + scoreAverage);
System.out.println("Number of scores above or equal to the average is " + getHighest(score));
System.out.println("Number of scores below the average is " + getLowest(score));
}
private static int scoreAverage;
public static int getHighest(int[] score)
{
int aboveAverage = 0;
for (int index = 1; index < score.length; index++)
{
if (score[index] >= scoreAverage)
{
aboveAverage++;
}
}
return aboveAverage;
}
public static int getLowest(int[] score)
{
int belowAverage = 0;
for (int index = 1; index < score.length; index++)
{
if (score[index] < scoreAverage)
{
belowAverage++;
}
}
return belowAverage;
}
}
大家好!现在我的代码有问题,我不知道为什么。该代码应该发回高于或等于平均分和低于平均分的分数。
不过,有时代码不会 return 正确的数字。例如,如果我输入 3 个考试成绩,分别是 100、90 和 80,平均分是 90。它应该显示 2 个是 above/equal 到 90,1 个低于 90。问题是,它显示0 低于 90。我已经对其进行了多次测试,它似乎只发生在低于平均水平。提前致谢!
在 getHighest(int[] score)
和 getLowest(int[] score)
方法中,数组的索引应该从 0 开始,而不是 1。这样您就错过了输入的第一个值。
您没有检查getHeighest()
和getLowest()
中第0
个索引中的元素。因此,您得到的 比 aboveAverage
或 belowAverage
.
删除行
private static int scoreAverage;
并将 scoreAverage
传递给两个函数。
代码:
public static int getHighest(int[] score, double scoreAverage)
{
int aboveAverage = 0;
//Index should start from 0 to avoid skipping the first element of the array.
for (int index = 0; index < score.length; index++)
{
if (score[index] >= scoreAverage)
{
aboveAverage++;
}
}
return aboveAverage;
}
public static int getLowest(int[] score, double scoreAverage)
{
int belowAverage = 0;
//Here also, index should start from 0.
for (int index = 0; index < score.length; index++)
{
if (score[index] < scoreAverage)
{
belowAverage++;
}
}
return belowAverage;
}
}
for (int index = 1; index < score.length; index++)
Java(以及大多数编程语言)中的数组使用 0-based numbering。你的循环应该从 int index = 0
开始,而不是 int index = 1
.
其他人已经回答说问题出在你的 for 循环中的索引应该是基于 0 的,这肯定是你代码中的一个错误。然而,不是问题所在。
在函数 getHighest()
和 getLowest()
中,变量 scoreAverage
指的是在定义函数之前声明的未初始化 class 级变量,即:
private static int scoreAverage;
不是函数 main()
中声明的 scoreAverage
。 class 级别变量默认为 0,这就是为什么您看不到任何低于平均值的值。
您可以通过删除 main()
中的 scoreAverage
声明并将 class 级别声明修改为:
private static double scoreAverage;
它必须是双精度数,而不是整数,因为除法 returns 是浮点类型,而不是整数。
或者,您可以使用在 main()
中声明的 double 类型的变量作为平均得分,并将其传递给两个函数,而不是访问 class 级别变量,这可能是更可取。
正如其他人所提到的,您的 for 循环应该从索引 0 开始:
for (int index = 0; index < scrore.length; index++)
但是您的代码还有另一个问题,您声明了两个名为 scoreAverage 的变量:一个在 main 方法中:
double scoreAverage;
还有一次在主要方法下面作为静态字段:
private static int scoreAverage
;
因此您只需要做一项更改:
private static double scoreAverage;
public static void main(String[] args)
{
int count = 0;
double scoreTotal = 0;
int index;
int tests;
//double scoreAverage; <--- remove that
double highest;
double lowest; ...
你的代码应该可以工作。
您调用的方法:getHighest() 和 getLowest() 没有正确的 scoreAverage 变量。在该方法的代码中,scoreAverage 变量等于 0。因此您可以这样做:
public static int getHighest(int[] score, double scoreAverage)
{
int aboveAverage = 0;
for (int index = 0; index < score.length; index++)
{
if (score[index] >= scoreAverage)
{
aboveAverage++;
}
}
return aboveAverage;
}
public static int getLowest(int[] score, double scoreAverage)
{
int belowAverage = 0;
for (int index = 0; index < score.length; index++)
{
if (score[index] < scoreAverage)
{
belowAverage++;
}
}
return belowAverage;
}