初学者Java程序员。我正在尝试制作一个程序,询问用户他们想要输入多少等级。然后,我想将用户输入的等级存储到数组中。最后,我想找到输入成绩的平均值,并打印出平均值以下每个等级的列表。截至目前,我的代码计算用户输入的成绩的平均值,但我无法弄清楚如何打印平均值以下的成绩。我相信问题在于我的最后一个循环,但我无法弄清楚如何解决它。另外,我是否正确实现了数组?感谢每一位花时间帮助我的人!
public static void grades() {
int q = 0;
double grades = 0;
double total = 0;
Scanner in = new Scanner(System.in);
// user input how many grades user would like to enter
System.out.println("How many grades would you like to enter? ");
q = in.nextInt();
// user enters # of grades they requested to enter
for (int counter = 0; counter < q; counter++) {
System.out.println("Enter your grades: ");
grades = in.nextInt();
// This creates an array that stores the grades the user entered
double[] scores = new double[] {grades};
// adds up all elements (grades)
for (int k = 0; k < scores.length; k++) {
total += scores[k]; //sums up entered grades
}
}
total = total / q; //calcs mean
//loops prints grades less than mean
for (grades = 0; grades < total; grades++){
System.out.println(grades);
}
}
答案 0 :(得分:1)
public static void grades(){
Scanner in = new Scanner(System.in);
System.out.println("How many grades would you like to enter? "); //user input how many grades user would like to enter
int q = in.nextInt();
double[] grades = new double[q];
double sum = 0;
for (int counter = 0; counter < q; counter++){ //user enters # of grades they requested to enter
System.out.println("Enter your grades: ");
double grade = in.nextInt();
grades[counter] = grade;
sum += grade;
}
double mean = sum / q;
System.out.println("Mean: " + mean);
for (int i = 0; i < q; i++){ //loops prints grades less than mean
if (grades[i] < mean) {
System.out.println(grades[i]);
}
}
}
答案 1 :(得分:0)
你的最后一个循环毫无意义
你应该计算数组中的所有项目,然后做一个IF来测试它是否低于平均值然后打印它,如果它低于平均值
你已经让循环从0重复到平均值......这没有任何意义,就像说一组学生的平均分数是30%现在打印从student0到student30的所有成绩30这没有任何意义去做。你应该对学生31和学生32感兴趣..不要停在30号学生.student1的成绩可能高于平均水平,而学生40的成绩可能低于平均水平。你将学生编号(数组中的位置索引)与成绩混合在一起。
<强> ADDED 强> 你实际上甚至比这更糟糕。因为等级甚至不是一个数组,它是一个带有一个数字的变量。你有一个数组增加一个变量并打印变量,如打印1,2,3,4,5
你打算做一个循环i = 1到n,并打印数组[i] 你正在做一个循环i = 1到n,你正在打印我
你应该弄清楚阵列是什么,而不是担心计算平均值。让你自己更简单的练习测试你做什么/不理解的东西,而不是书中的东西或老师给你的东西。并使用您的探索来理解您的老师/书给您的练习。
答案 2 :(得分:0)
您的代码存在一些问题。首先关闭你的数组,每次迭代都会输入一个新的等级,这将导致一个索引,因为之前的输入正在覆盖它。数组scores
应该在for循环之外初始化。
另外,用于打印成绩的for循环不正确。您需要迭代scores
数组并打印出每个单独的值。
以下代码适用并演示出错的地方:
public static void grades() {
int q = 0;
double grades = 0;
double total = 0;
Scanner in = new Scanner(System.in);
// user input how many grades user would like to enter
System.out.println("How many grades would you like to enter? ");
q = in.nextInt();
double[] scores = new double[q];//initialize here
// user enters # of grades they requested to enter
for (int i = 0; i < scores.length; i++) {
System.out.println("Enter your grades: ");
grades = in.nextInt();
// This creates an array that stores the grades the user entered
scores[i] = grades;//append values to each index
// adds up all elements (grades)
total += scores[i]; //sums up entered grades
}
total = total / q; //calcs mean
System.out.println("Mean: " + total);
//iterate through the scores array with filled values
for (int i = 0; i < scores.length; i++) {
System.out.println("Grade #" + i + ": " + scores[i]);
}
}
答案 3 :(得分:0)
可能是什么问题..总数和平均数是错误的。
import java.io.*;
class overloading2
{
String name;
int year;
String section;
String subject[];
double grade[];
double average;
public void enrolSubjects()throws Exception
{
DataInputStream mat=new DataInputStream(System.in);
try
{
System.out.println("Enter 3 subject:");
subject=new String[3];
for(int sub=0;sub<3;sub++)
{
subject[2]=(mat.readLine());
}
}
catch(IOException ioe)
{
}
}
public double enterGrades(double grade1,double grade2,double grade3)
{
DataInputStream math=new DataInputStream(System.in);
try
{
System.out.println("Enter 3 grades:");
grade=new double[3];
for(int x=0; x<3; x++)
{
grade[2]=Double.parseDouble(math.readLine());
}
}
catch(IOException ioe)
{
}
return enterGrades();
}
double enterGrades()
{
double total=0.0;
for(int x=0; x<3; x++)
{
total+=grade[2];
average=total/3;
}
return average;
}
public static void main(String []args)throws Exception
{
overloading2 ostud1=new overloading2();
double a=0.0,b=0.0,c=0.0;
ostud1.enrolSubjects();
ostud1.enterGrades(a,b,c);
System.out.println("average grade is "+ostud1.enterGrades());
}
}