2015-11-06 29 views
-3

因此,这里少一个是我的代码:Java的数量需要在回答

package e7; 
import java.util.Scanner; 
public class Q1 { 

    public static void main(String[] args) 
    { 
     double[] scores = new double[10]; 
     double sum = 0.0D; 
     int count = 0; 

     Scanner sc = new Scanner(System.in); 

     do { 
      System.out.print("Enter a new score (-1 to end): "); 
      scores[count] = sc.nextDouble(); 

      if (scores[count] >= 0.0D) 
       sum += scores[count]; 

     } 

     while (scores[(count++)] >= 0.0D); 

     System.out.println("The total number of scores is: " + count); 

     double average = sum/(count - 1); 
     int numOfAbove = 0; 
     int numOfBelow = 0; 

     for (int i = 0; i < count - 1; i++) { 
      if (scores[i] >= average) 
       numOfAbove++; 
      else 
       numOfBelow++; 
     } 

     System.out.printf("Average is " + "%.2f\n",average); 
     System.out.println("Number of scores above or equal to the average " + numOfAbove); 
     System.out.println("Number of scores below the average " + numOfBelow); 

    } 

    } 

如何让它显示计算的分数是否正确?如果我输入2个数字,然后做-1来结束它不断说3分数。应该只有两个。我该如何解决?由于

+0

我做了这个System.out.println(“总分数是:”+(count-1));但它似乎相当贫民窟,所以我认为这可能是另一种方式 – LeroyJenkins

回答

2
System.out.println("The total number of scores is: " + count); 

你可能想:

System.out.println("The total number of scores is: " + (count - 1)); 
0

你也可以从一个做改变你的循环,而while循环如下,

while (true) { 
    System.out.print("Enter a new score (-1 to end): "); 
    double tempDouble = sc.nextDouble(); 

    if (tempDouble >= 0.0D) 
    scores[count] = tempDouble; 
    sum += scores[count]; 
    count++; 
    else 
    break; 
} 

这样一来,就好像你的双输入是不正确的,它会在用户输入-1时跳出while循环。你可能需要为你的用例调整一下。