-4

我的程序应该找到学生的女性、男性和总平均 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);
  }
}
4

2 回答 2

2

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

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

其他跳出来的问题是:

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

  • 你计算allAverage不正确。再看一遍代码。问题应该很明显。


实际上,回答这个问题的困难之一是完全不清楚编写的程序应该如何表现。我们无法从您向我们展示的内容中推断出这一点。'因为你写的显然行不通……从可用性的角度来看。

如果您不理解并且无法正确解释需求,那么您将能够正确实现它们的机会不大。

于 2013-03-20T08:14:21.810 回答
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);

}

于 2013-03-20T09:21:46.837 回答