如何在Java中使用循环和平均值

时间:2013-03-20 08:02:33

标签: java loops

我的课程应该能够找到学生的平均女性,男性和平均GPA。还有女性,男性和学生总数。首先,它询问学生是男性还是女性。如果你选择男性它会循环,但在它结束后。我希望我的程序直接进入下一个选择。例如,如果您选择男性,您将输入女性,反之亦然。

import java.util.Scanner;
public class practice {
  public static void main(String [] args) {
    Scanner keyboard = new Scanner (System.in);
    int maleCount=0, femaleCount=0, totalStudents;
    double GPA, mTotal = 0, mAverage, fTotal = 0, fAverage, allAverage;

    System.out.println("Is the student Male or Female?");
    System.out.println("Enter M for male or F for female.");
    String student = keyboard.next().toUpperCase();
    System.out.println("Enter GPA");
    GPA = keyboard.nextDouble();


    if (student.equals("M")) {
      while (GPA >=0) {
        mTotal = mTotal + GPA;
        maleCount++;
        GPA = keyboard.nextDouble();
      }
    }

    if (student.equals("F")) {
      while (GPA >=0) {
        fTotal = fTotal + GPA;
        femaleCount++;
        GPA = keyboard.nextDouble();
      }
    }

    mAverage = mTotal/maleCount;
    fAverage = fTotal/femaleCount;
    allAverage = mTotal + fTotal;
    totalStudents = maleCount + femaleCount;

    System.out.println("Total MALE students: " + maleCount);
    System.out.println("Total FEMALE students: " + femaleCount);
    System.out.println("Total STUDENTS: " + totalStudents);
    System.out.println("Total MALE GPA: " + mTotal);
    System.out.println("Total FEMALE GPA: " + fTotal);
    System.out.println("Total MALE Average GPA: " + mAverage);
    System.out.println("Total average: " + allAverage);
  }
}

2 个答案:

答案 0 :(得分:2)

  

如何在Java中使用循环和平均值?

好吧,就像你问题中的代码一样,我会说。只需在需要循环的部分周围添加一个循环,并弄清楚如何结束循环。

向我跳出的其他问题是:

  • 您似乎以奇怪的顺序接受输入。

  • 您正在错误地计算allAverage。再看看代码吧。问题应该是显而易见的。


实际上,回答这个问题的一个困难是,根本不清楚所编写的程序是如何表现的。我们无法从你向我们展示的东西中推断出这一点。从可用性的角度来看,你写的东西显然不起作用。

如果您不理解并无法正确解释要求,那么您很可能无法正确实施这些要求。

答案 1 :(得分:0)

修复了我的代码,不好意思。

import java.util.Scanner;
public class practice 
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner (System.in);
int maleCount=0, femaleCount=0, totalStudents, count = 0;
double GPA, mTotal = 0, mAverage, fTotal = 0, fAverage, allAverage;

System.out.println("Is the student Male or Female?");
System.out.println("Enter M for male or F for female.");
String student = keyboard.next().toUpperCase();


do{
  System.out.println("Enter GPA " + student);
  GPA = keyboard.nextDouble();
if (student.equals("M"))
{
while (GPA >=0)
{
    mTotal = mTotal + GPA;
    maleCount++;
    GPA = keyboard.nextDouble();
}
student = "F";
}


else if (student.equals("F"))
{
    while (GPA >=0)
    {
        fTotal = fTotal + GPA;
        femaleCount++;
        GPA = keyboard.nextDouble();
    }
    student = "M";
}
}
while (++count < 2);


mAverage = mTotal/maleCount;
fAverage = fTotal/femaleCount;
totalStudents = maleCount + femaleCount;
allAverage = (mTotal + fTotal)/totalStudents;



System.out.println("Total MALE students: " + maleCount);
System.out.println("Total FEMALE students: " + femaleCount);
System.out.println("Total STUDENTS: " + totalStudents);
System.out.println("Total MALE GPA: " + mTotal);
System.out.println("Total FEMALE GPA: " + fTotal);
System.out.println("Total average: " + allAverage);

}