最小值和最大值

时间:2015-01-29 20:29:19

标签: java

我必须编写代码来查找成绩的最大,最小和平均。这就是我到目前为止所做的:

public class test
{
public static void main (String[] args)
{
double  average, count = 0, sum = 0, grades, max=0, min=0;
final int = MAX_GRA = 0
final int = MIN_GRA = 0

Scanner scan = new Scanner (System.in);

System.out.print ("Enter the grades (999 to quit): "); //Taking input from the user
grades = scan.nextInt();

while (grades != 999) //A while loop that doubles count and adds the sum to grades
  {
  count++;
  sum += grades;

  System.out.println ("The sum so far is " + sum);
  System.out.print ("Enter the grades (999 to quit): ");

  grades = scan.nextInt();
  }

System.out.println ();

if (count == 0) //An if statement that identifies if no numbers were entered
System.out.println ("No grades were entered.");

else //An else statement that computes that computes the average
  {
  average = (double)sum / count;
  DecimalFormat fmt = new DecimalFormat ("0.##");
  System.out.println ("The average grade is " + fmt.format(average));
  }
 }   
}

有什么想法吗?我是一般的java和编码新手。谢谢你的时间!

3 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

int maxGrade = 0;
int minGrade = 10; // Set the highest possible grade.

while (grades != 999)
{
    // If the current grade is greater that the maxGrade,
    // set this value the maxGrade.
    if(grades > maxGrade) 
        maxGrade = grades;

    // If the current grade is less than the minGrade,
    // set this value to the minGrade.
    if(grades < minGrade)
        minGrade = grades;
    // Here you will place the rest of your code. 
}

注意:尝试为变量使用更有意义的名称。这将使您的代码更具可读性,并且您可以更轻松地向代码读者传达您的意图和想法。例如,由于grades会保留当前grade的值,因此将其命名为grade会更合理。

<强>更新

正如下面提到的那样,你可以使用Math类的两种方法,以避免if语句。

while (grades != 999)
{
    maxGrade = Math.max(maxGrade, grades);

    minGrade = Math.min(minGrade, grades);
}

答案 1 :(得分:0)

我会更改原始代码中的某些内容,并按此放置。

public class Grades {

    // MODIFIERS (final, private, protected, static, etc.) type (int, double,
    // float, Object, etc.) name [= initialValue];
    private final static double END_GRADES = 999;

    public static void main(String[] args) {

        double count, sum, grades, max, min;

        Scanner scan = new Scanner(System.in);

        // Initialized data.
        count = 0;
        sum = 0;
        max = 0;
        min = 0;

        do {
            System.out.println("Enter next grade ("+(int)END_GRADES+" to quit):");
            grades = scan.nextDouble();

            if (grades != END_GRADES) {

                if (count == 0) { // First grade is always min.
                    min = grades;
                }

                sum = sum + grades;
                count++;
                System.out.println("The sum so far is: " + sum);

                if (max < grades) { // New max??
                    max = grades;
                }

                if (min > grades) { // New min??
                    min = grades;
                }
            }

        } while (grades != END_GRADES);

        if (count != 0) {
            System.out.println("The average grade is: " + (sum / count));
            System.out.println("The max grade is: " + max);
            System.out.println("The min grade is: " + min);
        } else {
            System.out.println("No grades were entered.");
        }
    }
}

答案 2 :(得分:0)

看起来你走了一半的答案,但我认为你错过了 min max 值,因为你已经计算了平均值和总和。

首先,你需要添加两条指令来更新你的最大和最小变量等级持有者,简短的方法是使用:

  • Math#max简写比较返回参数的最大值。
  • Math#min简写比较返回参数的最小值。

但您必须删除finalMAX_GRA前面的MIN_GRA修饰符,因为这些实例变量需要在while循环的每次迭代中更新,以便他们得到用户输入的成绩或保持自己的价值。

然后您应该处理变量声明,以便进行比较并且这样做,您可以将min值设置为整数范围内的最大值,反之亦然。

最后,您需要添加print语句,以显示最大/最小值以及您的平均成绩:

public class MinMaxAndAverageCalculator
{
  public static void main (String[] args)
  {
    double  average, count = 0, sum = 0, max=0, min=0;
    int MAX_GRA = Integer.MIN_VALUE;
    int MIN_GRA = Integer.MAX_VALUE;
    int grade;

    Scanner scan = new Scanner (System.in);

    System.out.print ("Enter the grades (999 to quit): "); //Taking input from the user
    grade = scan.nextInt();

    while (grade != 999) //A while loop that doubles count and adds the sum to grades
    {
      count++;
      sum += grade;
      MAX_GRA = Math.max(grade, MAX_GRA);
      MIN_GRA = Math.min(grade, MIN_GRA);
      System.out.println ("The sum so far is " + sum);
      System.out.print ("Enter the grades (999 to quit): ");
      grade = scan.nextInt();
    }

    if (count == 0) //An if statement that identifies if no numbers were entered
    {
      System.out.println("No grades were entered.");
    }
    else //An else statement that computes that computes the average
    {
      average = (double) sum / count;
      DecimalFormat fmt = new DecimalFormat ("0.##");
      System.out.println ("The average grade is " + fmt.format(average));
      System.out.println ("The maximum grade is " + MAX_GRA);
      System.out.println ("The minimum grade is " + MIN_GRA);
    }
  }
}
相关问题