我是一名Java初學者,被我的任務停下來編寫一個代碼,可以找到輸入的平均值,最小值,最大值和範圍。這是我的所有代碼,並沒有完成。平均出來很好,但我寫的最低和最高不讀。任何人都可以幫助我將最小和最大連接到我的輸入?查找平均值,最小值,最大值和範圍
package averages;
import java.util.Scanner;
public class Averages {
public static void main(String[] args) {
//initialize variables
double sum =0;
int count = 0;
double grade =0;
//initialize scanner and input
System.out.print("Enter Grades, enter -1 to end the set:");
Scanner in = new Scanner (System.in);
//Process data until sentinel is entered
while (grade != -1)
{
grade = in.nextDouble();
if (grade != -1)
{
sum = sum + grade;
count++;
}
}
// Finding the average
if(count > 0)
{
double average = sum/ count;
System.out.println("Grade Average: "+average);
}
else
{
System.out.println("No Data");
}
//Finding largest grade
double largest = in.nextDouble();
while(in.hasNextDouble())
{
double input = in.nextDouble();
if(input > largest)
{
largest = input;
}
System.out.println(largest);
//Finding smallest grade
double smallest = in.nextDouble();
while (in.hasNextDouble())
{
double input1 = in.nextDouble();
if(input1 < smallest)
{
smallest = input;
}
System.out.println(smallest);
}}}}
看一看用於文檔[nextDouble()](http://docs.oracle.com /javase/7/docs/api/java/util/Scanner.html#nextDouble%28%29)。 –